Internet Explorer 10 and popstate not binding

2019-03-30 03:39发布

问题:

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?

EDIT (Gustin): + Sample Code

PopStateEvent.js (complete listing, using jQuery 1.9.1):

$(window).bind('popstate', function () { alert("Pop state event received!"); });

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.

With Chrome 24, everything works fine.

回答1:

Are you sure that you aren't calling it using history.pushState() or history.replaceState()?

popstate is only triggered when you do something with the browser, like the back button , or called using history.back().

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();
}

I believe popstate is included in document.readyState, so give it a shot.