为什么
window.addEventListener('popstate', () => alert('pop'));
window.history.pushState(null, '', '/foo');
不提醒 pop
?
注意:在最新的 chrome 上测试
--
根据 MDN :
A popstate event is dispatched to the window every time the active history entry changes. If the history entry being activated was created by a call to pushState or affected by a call to replaceState, the popstate event's state property contains a copy of the history entry's state object.
那么为什么我的pushState
没有触发popstate
事件呢?
最佳答案
您可以在每次调用history.pushState()
时手动触发window
上的popstate
事件。
history.pushState(state, '', url);
var popStateEvent = new PopStateEvent('popstate', { state: state });
dispatchEvent(popStateEvent);
关于html - history.pushState 不触发 'popstate' 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10940837/