history.pushState does not trigger 'popstate' event

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

Why

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

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

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 12:18

    Document https://developer.mozilla.org/en-US/docs/Web/Events/popstate says:

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

提交回复
热议问题