以编程方式更新哈希而不触发 hashchange 事件?
我正在使用 jQuery BBQ 插件将状态推送到 location.hash。
为了防止反馈循环,我想在以编程方式设置状态时暂时禁用 hashchange 侦听器。
我见过这个解决方案: 更改哈希值而不触发 hashchange 事件
不幸的是,这似乎并不存在完全健壮,因为即使我这样做,它有时也会触发:
updateURL(obj){
$(window).unbind( 'hashchange');
$.bbq.pushState(obj);
setTimeout( function() { bindHashChange()}, 500);
}
现在有更好的方法以编程方式推送状态吗?也许另一个 JS 库?
I'm using the jQuery BBQ plugin to push states to the location.hash.
To prevent a feedback loop, I'd like to disable the hashchange listener temporarily while setting the state programmatically.
I've seen this solution:
Change hash without triggering a hashchange event
Unfortunately, it doesn't seem to be perfectly robust as it sometimes triggers even though I do this:
updateURL(obj){
$(window).unbind( 'hashchange');
$.bbq.pushState(obj);
setTimeout( function() { bindHashChange()}, 500);
}
Is there now a better approach to push states programmatically? Perhaps another JS library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
超时执行此操作不会可靠地工作。链接答案的重要部分是设置一个您的处理程序知道的标志。请参阅更新的代码问题。
或者,将临时处理程序绑定到负责重新建立处理程序的事件:
Doing this with a timeout will not work reliably. The important part of the linked answer is setting a flag that your handler knows about. See the updated question for code.
Alternatively, bind a temporary handler to the event that is in charge of reestablishing your handler: