history.pushState不会触发'popstate'事件 [英] history.pushState does not trigger 'popstate' event
问题描述
为什么
$(function () {
$(window).bind('popstate', function () {alert('pop');});
window.history.pushState(null, '', '/foo');
});
不提醒 pop
?
注意:测试最新的镀铬
-
根据 MDN :
每次活动历史记录条目更改时,都会将popstate事件分派到窗口。如果正在激活的历史记录条目是通过调用pushState创建的或受对replaceState的调用影响而创建的,则popstate事件的state属性包含历史记录条目的状态对象的副本。
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
事件?
So why my pushState
does not trigger the popstate
event?
推荐答案
你引用的段落有点含糊不清。阅读同一页面上的示例,很明显只有当用户点击后面时才触发popstate按钮,而不是当脚本调用 pushState()
时。
The paragraph you reference is a little ambiguous. Reading the example on the same page, it is clear that popstate is only triggered when the user clicks the back button, not when the script calls pushState()
.
这篇关于history.pushState不会触发'popstate'事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!