history.pushState does not trigger 'popstate' event

前端 未结 7 608
旧巷少年郎
旧巷少年郎 2020-12-16 12:13

Why

$(function () {
  $(window).bind(\'popstate\', function () {alert(\'pop\');});

  window.history.pushState(null, \'\', \'/foo\');
});

7条回答
  •  萌比男神i
    2020-12-16 12:29

    Don't know exactly what you where trying to achieve but if you simply want your popState Event Handler to launch you can simply extract it out to a function like below :

    function popStateHandler(event) {
         // Event handling Code here
    }
    window.onpopstate = popStateHandler;
    

    Then you can simply call your popStateHandler after you pushState.

提交回复
热议问题