检测URL上的更改 [英] Detect changes on the url

查看:84
本文介绍了检测URL上的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些东西来检测URL的变化,但是页面不会回发,它会动态地改变html的内容,所以URL页面就像这样


http://www.examplepage/conversations/44455

当我点击页面的另一部分时,它是

< blockquote>

http://www.examplepage/conversations/44874


它不仅在最后变化,而且像这样


http://www.examplepage/settings


没有做回发和重新加载的JavaScript所以我的问题是,有没有办法来检测这些变化?和事件监听器,但如何?



我搜索了每个人都说的哈希事件,但是我没有任何哈希值,所以它不起作用。

编辑

只是为了记录我没有页面代码也没有访问权限,我会更好地解释,我正在做一个后台谷歌扩展和我只是向现有页面添加了一张幻灯片,该页面按照上面解释的方式更改了它的网址。 url会像每个页面一样改变,但它们会改变html内部的div,以便页面不必再次收取所有内容。

解决方案

您需要在页面加载时将URL存储为起点,并使用 setInterval n毫秒

以下代码会每秒检查两次(500ms):

pre> //在加载时存储网址
var currentPage = window.location.href;

//监听变化
setInterval(function()
{
if(currentPage!= window.location.href)
{
//页面已更改,将新页面设置为'current'
currentPage = window.location.href;

//执行您的操作...
}
},500);


I need something to detect changes on the url but the page doesn't do post back, it changes dynamically adding just the div inside the html, so the URL page is something like this

http://www.examplepage/conversations/44455

and when I click on another section of the page it it is

http://www.examplepage/conversations/44874

it changes not only at the end but like this

http://www.examplepage/settings

without doing post back and reloading the javascript so my question is, is there a way to detect those changes? and event listener but how?

I search and everyone says hash event but I don't have any value after any hash so it doesn't work

EDIT

Just for the record I have no code of the page nor I have access, I'll explain better, I am doing a background google extension and I just added a slide out to an existing page, this page changes its url the way I explained above. the url changes like every page does, but they change the div inside the html so that the page doesn't have to charge everything again

解决方案

You need to store the URL when the page loads as a starting point and check every n milliseconds for changes using setInterval then modify based on that.

The following code does this check twice a second (500ms):

// store url on load
var currentPage = window.location.href;

// listen for changes
setInterval(function()
{
    if (currentPage != window.location.href)
    {
        // page has changed, set new page as 'current'
        currentPage = window.location.href;

        // do your thing...
    }
}, 500);

这篇关于检测URL上的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆