如何检测窗口哈希的变化?

发布于 2024-10-12 19:38:06 字数 307 浏览 8 评论 0原文

如何检测 window.location.hash onchange,例如我可以这样做:

if(window.location.hash.hasChanged())
{
   // ajax stuff
}
else
{
   // nothing, no hash has been changed (without any window reload)
}

如果我更改哈希值,例如 编辑并在编辑后按 Enter 键更改哈希值什么也没有发生,只有在窗口重新加载时才会检测到哈希值的变化。

How can you detect a window.location.hash onchange, for example I could do this:

if(window.location.hash.hasChanged())
{
   // ajax stuff
}
else
{
   // nothing, no hash has been changed (without any window reload)
}

If I change the hash, e.g. edits and change the hash by pressing enter after editing nothing happens, only on a window reload will detect a change in the hash.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走走停停 2024-10-19 19:38:06

最新的浏览器(FF3.6+、IE8、Chrome)支持 window 对象上的“hashchange”事件 - 请参阅 MDC: window.onhashchange 了解使用示例。简单的异步监听可能看起来像:

window.onhashchange = function() {
   // do something awesome here
};

如果您想要一个支持旧版浏览器的实现,事情很快就会变得复杂,我建议使用其他人的库或插件 - 请参阅答案 针对此问题寻求一些建议。

Most recent browsers (FF3.6+, IE8, Chrome) support the "hashchange" event on the window object - see MDC: window.onhashchange for usage examples. Simple asynchronous listening might look like:

window.onhashchange = function() {
   // do something awesome here
};

If you want an implementation that supports older browsers, things get complicated quite quickly, and I recommend using someone else's library or plugin - see answers to this question for several suggestions.

任性一次 2024-10-19 19:38:06

Ben Alman 的 hashchange jQuery 插件 提供了监视哈希更改的功能。

Ben Alman's hashchange jQuery plugin provides the ability to monitor the hash for changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文