hashchange firing popstate

 ̄綄美尐妖づ 提交于 2019-12-05 12:38:33

问题


Here's what I'm working with

Code:

<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>​

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?


回答1:


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.

From https://developer.mozilla.org/en/DOM/window.onpopstate :

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.



来源:https://stackoverflow.com/questions/9948510/hashchange-firing-popstate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!