onpopstate事件未在window.history.forward()上触发

时间:2017-12-09 22:27:58

标签: javascript event-listener

我正在尝试使用History API并遇到了一个无法解决的问题。将一个新状态推送到api后,我无法在window.history.forward()上模拟popstateEvent(模拟浏览器前进按钮的点击)。使用window.history.back()一切都按预期工作。我想知道为什么会发生这种情况。这是为了吗?

window.onpopstate = function(event) {
    console.log(event);
};

window.history.pushState({'state': 'test'}, '', '/testurl');

window.history.go(1);
window.history.back();

window.onpopstate = function(event) {
    console.log(event);
};

window.history.pushState({'state': 'test'}, '', '/testurl');

window.history.go(1);
window.history.forward();

资源:

  1. https://developer.mozilla.org/en-US/docs/Web/API/Window/history
  2. https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onpopstate
  3. https://developer.mozilla.org/en-US/docs/Web/API/History_API
  4. 尝试浏览器: 最新的FireFox Quantum Beta 最新的Google Chrome稳定版

0 个答案:

没有答案