问题描述
我想知道什么会导致window.addEventListner('popstate', foo, false) window.attachEvent('onpopstate', foo)
和window.onpopstate=foo
不触发函数foo.我们有与此类似的代码,在生产中不会触发,而在开发中会触发.是否可以通过JS禁用popstate事件或导致某种冲突或竞态条件发生?
I was wondering what could cause window.addEventListner('popstate', foo, false) window.attachEvent('onpopstate', foo)
and window.onpopstate=foo
to not trigger the function foo. We have code similar to this and in production it does not fire but in development it does. Is it possible to disable the popstate event via JS or cause some sort of collision or race condition for this to occur?
PopStateEvent.js(完整列表,使用jQuery 1.9.1):
$(window).bind('popstate', function () { alert("Pop state event received!"); });
如果我正在使用 IE 10 (10.0.9200.16484)测试这个非常简单的示例,那么我将永远不会收到弹出状态事件(即永远不会出现警告框).即使我离开浏览器并通过IE的后退按钮回来也没有. IE. 此问题不能解决我的情况.
If I'm testing this very simple example with IE 10 (10.0.9200.16484), I never receive a pop state event (i.e. the alert box never appears). Not even if I navigate away and come back via the back button of IE. I.e. this Q/A doesn't solve my case.
使用 Chrome 24 ,一切正常.
推荐答案
您确定不使用history.pushState()或history.replaceState()调用它吗?
Are you sure that you aren't calling it using history.pushState() or history.replaceState()?
仅当您使用浏览器执行某些操作(例如后退按钮或使用history.back()调用)时,才会触发popstate.
popstate is only triggered when you do something with the browser, like the back button , or called using history.back().
除此之外,它可以与chrome一起使用,因为加载页面时它将拍摄popstate,因此请检查以确保实际加载了事件.对此进行检查应包含在
other than that, it will work with chrome because it'll shoot a popstate when the page is loaded, so check to make sure the event is actually being loaded. Checking for this should be included in the :
if (document.readyState == "complete") {
initApplication();
}
我相信popstate包含在document.readyState中,因此请尝试一下.
I believe popstate is included in document.readyState, so give it a shot.
这篇关于Internet Explorer 10和popstate未绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!