绑定窗口到 popstate 事件触发两次
只需将窗口(在 jQuery 中)绑定到 popstate,该事件总是会触发两次。
$( window ).bind( 'popstate', myFunction );
myFunction() 中没有任何东西会导致这种情况 - 我尝试将此函数剥离为简单的:
console.log( 'triggered' );
结果是相同的。它总是触发两次(在 Safari、Chrome 和 Firefox 中测试)。
我知道 HTML5 历史记录 API 存在问题,但除了“尝试 History.js”之外的任何建议,我们将不胜感激!
Simply binding the window (in jQuery) to popstate, the event is always triggered twice.
$( window ).bind( 'popstate', myFunction );
There's nothing in myFunction() to cause this - I've tried stripping out this function to a simple:
console.log( 'triggered' );
And the result is the same. It's always triggered twice (tested in Safari, Chrome & Firefox).
I know there are issues with the HTML5 history API, but any advice other than 'try History.js' would be gratefully received!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,我想出了一个办法来解决这个问题:
只触发一次函数,但状态仍然触发两次!
Ok, I figured out a way to solve this:
Only triggers the function once, but the state is still triggered twice!
这并没有直接回答这个问题,但可能对发现这个问题的其他人有帮助。因为我只需要监听一次popstate,然后立即删除监听器。
如果想在监听器触发后(仅一次)将其释放,可以将以下对象添加到 addEventListener 的第三个参数中:
这是文档: https://developer.mozilla.org /en-US/docs/Web/API/EventTarget/addEventListener
This does not answer the question directly, but it might be of help to others who find this question. Because I only needed to listen for the popstate once, and remove the listener immediately.
If you want to dispose of the listener after it has been triggered (only once), you can add the following object to the third parameter of addEventListener:
Here is the documentation: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
使用history API时,popstate事件被触发两次,即histry.back()和history.go(-1)。但仅使用浏览器后退按钮一次。
我使用超时作为解决方法:
the popstate event is triggered twice when using the history API i.e. histry.back() and history.go(-1). but just once using the browser back button.
I use a timeout as a workaround:
我在处理 ReactJS 中的后退按钮时遇到以下问题
以下代码
以解决问题 1 我执行了 解决问题2 我在下面的代码中做了,
不要忘记在构造函数中添加 this.isBackButtonClicked = false; 并取消事件。
I having below problems when handling the back button in reactjs
to Solve problem 1 I did the following code
to solve problem 2 I did below code,
don't forget to add this.isBackButtonClicked = false; in constructor and unscubscribe the events.