hashchange触发popstate [英] hashchange firing popstate
问题描述
代码:
<a href="#" onclick="window.onpopstate = function() { alert('pop'); };
return false; ">set up window.onpopstate
</a><br>
<a href="#somehash2">change hash</a>
<div onclick="alert(location.href);">show location.href</div>
为什么单击change hash
链接会触发popstate,难道不应该仅在单击change hash
链接然后单击返回时才触发popstate吗?
Why does clicking the change hash
link fire the popstate, shouldn't it only be fired if I click the change hash
link then click back?
推荐答案
window.onpopstate
触发的原因不是由于哈希值的更改.
这是因为单击锚标记后,历史记录已更改.
The reason window.onpopstate
fires are not because of a change to the hash.
It's because the history has been changed when you click on the anchor tag.
来自 https://developer.mozilla.org/en/DOM/window.onpopstate :
每次活动状态下,都会将popstate事件调度到窗口 历史记录条目更改.如果激活的历史记录条目是 是由对history.pushState()的调用创建的,或者受了对的调用的影响 history.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 history.pushState() or was affected by a call to history.replaceState(), the popstate event's state property contains a copy of the history entry's state object.
这篇关于hashchange触发popstate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!