如何检测窗口哈希的变化?
如何检测 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最新的浏览器(FF3.6+、IE8、Chrome)支持 window 对象上的“hashchange”事件 - 请参阅 MDC: window.onhashchange 了解使用示例。简单的异步监听可能看起来像:
如果您想要一个支持旧版浏览器的实现,事情很快就会变得复杂,我建议使用其他人的库或插件 - 请参阅答案 针对此问题寻求一些建议。
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:
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.
Ben Alman 的 hashchange jQuery 插件 提供了监视哈希更改的功能。
Ben Alman's hashchange jQuery plugin provides the ability to monitor the hash for changes.