我一直在玩 window.history.pushState 和 onpopstate 但在我看来,按下返回会跳回两个状态,跳过一个。
window.onpopstate = function(event) {
alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
};
history.pushState({page: 1}, "title 1", "?page=1");
history.pushState({page: 2}, "title 2", "?page=2");
history.replaceState({page: 3}, "title 3", "?page=3");
history.back(); // alerts "location: http://example.com/example.html?page=1, state: {"page":1}"
history.back(); // alerts "location: http://example.com/example.html, state: null
history.go(2); // alerts "location: http://example.com/example.html?page=3, state: {"page":3}
History.js 似乎表现出相同的行为。请参阅“直接使用 History.js”部分中的https://github.com/browserstate/history.js 。它在第二个 back() 中从状态 3 跳转到状态 1。
那么为什么它有这种行为或者我错过了什么?