history.pushState does not trigger 'popstate' event

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

Why

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

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

7条回答
  •  被撕碎了的回忆
    2020-12-16 12:30

    My solution:

    var url = "http://awesome.website.net/route/to/paradise";
    window.history.pushState({}, "", url);
    window.history.pushState({}, "", url); // yes twice
    window.history.back();
    

    It will trigger a soft-navigation via 'popstate' event and no HTTP request.

提交回复
热议问题