为什么pushstate触发了popstate?

时间:2016-07-01 15:33:39

标签: javascript jquery html5 history.js

我正在使用history.js

 https://github.com/browserstate/history.js/ 

HTML5历史/州API。由于调用history.pushState()或history.replaceState()时文档示例https://developer.mozilla.org/en-US/docs/WindowEventHandlers.onpopstate不会触发popstate事件。只有通过执行浏览器操作(例如单击后退按钮(或在JavaScript中调用history.back())才能触发popstate事件。只有当用户在同一文档的两个历史记录条目之间导航时才会触发事件。

(function(window,undefined){

    // Bind to StateChange Event
    History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
        var State = History.getState(); // Note: We are using History.getState() instead of event.state
        window.location.href // reload page
    });

    // Change our States
    History.pushState({state:1}, "State 1", "?state=1"); // logs {state:1}, "State 1", "?state=1"
    History.pushState({state:2}, "State 2", "?state=2"); // logs {state:2}, "State 2", "?state=2"
    History.replaceState({state:3}, "State 3", "?state=3"); // logs {state:3}, "State 3", "?state=3"
    History.pushState(null, null, "?state=4"); // logs {}, '', "?state=4"
    History.back(); // logs {state:3}, "State 3", "?state=3"
    History.back(); // logs {state:1}, "State 1", "?state=1"
    History.back(); // logs {}, "Home Page", "?"
    History.go(2); // logs {state:3}, "State 3", "?state=3"

})(window);

但是当我添加history.js并调用history.pushState()或history.replaceState()时,它每次都会触发一个popstate / statechange事件。和ajax无法正常工作,页面重新加载。我发现了一些讨论[https://github.com/browserstate/history.js/issues/312 / https://github.com/browserstate/history.js/issues/96],但没有找到解决方案。

0 个答案:

没有答案