如何检测在 JavaScript 中散列后 URL 是否已更改 [英] How to detect if URL has changed after hash in JavaScript
问题描述
如何在 JavaScript 中检查 URL 是否已更改?例如,使用 AJAX 的 GitHub 等网站将在 # 符号后附加页面信息以创建唯一的 URL,而无需重新加载页面.检测此 URL 是否更改的最佳方法是什么?
- 是否再次调用了
onload
事件? - 是否有 URL 的事件处理程序?
- 还是必须每秒检查一次 URL 以检测更改?
在现代浏览器(IE8+、FF3.6+、Chrome)中,你可以只在 上监听
.hashchange
事件窗口
在一些旧浏览器中,您需要一个持续检查 location.hash
的计时器.如果您使用 jQuery,则有一个 plugin 可以做到这一点.>
How can I check if a URL has changed in JavaScript? For example, websites like GitHub, which use AJAX, will append page information after a # symbol to create a unique URL without reloading the page. What is the best way to detect if this URL changes?
- Is the
onload
event called again? - Is there an event handler for the URL?
- Or must the URL be checked every second to detect a change?
In modern browsers (IE8+, FF3.6+, Chrome), you can just listen to the hashchange
event on window
.
In some old browsers, you need a timer that continually checks location.hash
. If you're using jQuery, there is a plugin that does exactly that.
这篇关于如何检测在 JavaScript 中散列后 URL 是否已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!