使用javascript从IE8地址栏中查找当前url
当页面加载时,我更改了 url 中的书签。这样,当用户单击浏览器的后退按钮时,浏览器实际上不会返回,而是会更改书签。然后我可以检测书签更改并执行其他操作作为对用户按下后退按钮的反应。
我的问题是找到当前的网址,包括任何书签/哈希更改。它适用于所有浏览器,使用以下组合,但不适用于 IE8:
document.URL
location.href
window.location.hash
和 window.onhashchange
由于它不适用于 IE8,任何人都可以为我指出正确的方向我可以检测 IE8 中的书签/哈希更改吗?
I change the bookmark in the url when the page is loaded. That way, when the user clicks the back button of the browser, the browser will not actually go back, but will instead change the bookmark. I can then detect the bookmark change and do something else as a reaction to the user pressing the back button.
My problem is to find the current url, including any bookmark/hash changes. It works in all browsers, using a combination of the following, but not in IE8:
document.URL
location.href
window.location.hash
and the window.onhashchange
As it doesn't work for IE8, could anyone possibly point me in the right direction towards how I can detect the bookmark/hash change in IE8?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看 jquery-bbq,因为它实现了 hashchange 并使其在 IE6 中工作 -现在是 IE8,可能还有 IE9。您可能可以完全依赖它而不是您的自定义代码。
Look at jquery-bbq, as it implements the hashchange and makes it work in IE6-IE8 and possibly IE9 now. You could probably entirely rely on it instead of your custom code.
根据jQuery hashchange插件的作者介绍,IE8支持绑定到
window.onhashchange 开箱即用的事件。
也许您可以尝试使用 Ben 的插件,该插件旨在在较旧的浏览器上启用哈希更改检测。
编辑:我的测试显示该事件在 IE8 中没有触发。然后我在上面插件的源代码中找到以下注释:
显然,我正在运行 documentMode 5 这是怪异模式。我敢打赌它只能在 IE8 标准模式下工作。无论如何,您应该能够实现相同的 JS Ben 使用的代码。
According to the author of the jQuery hashchange plugin, IE8 supports binding to the
window.onhashchange
event out of the box.Perhaps you could try using Ben's plugin, which is intended to enable hash change detection on older browsers.
EDIT: My tests showed that the event is not firing in IE8. Then I found the following comment in above plugin's source code:
Apparently, I'm running in documentMode 5 which is quirks mode. I bet it works in IE8 standards mode only. Regardless, you should be able to implement the same JS code that Ben used.