最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - hashchange firing popstate - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Mar 30, 2012 at 18:34 qwertymk asked Mar 30, 2012 at 18:27 qwertymkqwertymk 35.3k30 gold badges124 silver badges184 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 21

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.

发布评论

评论列表(0)

  1. 暂无评论