popstate event handler seems not to work

前端 未结 3 1001
栀梦
栀梦 2020-12-11 16:02

I\'m having a problem with a \'popstate\' event handler, this is my code:

window.addEventListener(\"popstate\", function (event){
    if (event.state) {
             


        
相关标签:
3条回答
  • 2020-12-11 16:05

    history.pushState() will not trigger the popstate event by definition.

    The popstate event is fired in certain cases when navigating to a session history entry.

    This event is intended to be only triggered when navigating to a history entry, either by pressing the back/forward button, or history.go/back.

    0 讨论(0)
  • 2020-12-11 16:07

    pushState do not trigger the popstate event, only clicking back / forward button (or use backspace) or invoking history.back() / history.go(n) would trigger this event.

    Also, in webkit browsers, a popstate event would be triggered after page's onload event, but Firefox and IE do not have this behavior.

    0 讨论(0)
  • 2020-12-11 16:16

    No it will not,

    As per MDN documentation

    Note that just calling history.pushState() or history.replaceState() won't trigger a popstate event. The popstate event is only triggered by doing a browser action such as a click on the back button (or calling history.back() in JavaScript).

    Again as per this question the popstate event is not triggered in Chrome when you call pushState().

    0 讨论(0)
提交回复
热议问题