Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hashchange firing popstate

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?

like image 276
qwertymk Avatar asked Mar 30 '12 18:03

qwertymk


People also ask

What triggers Popstate?

The popstate event of the Window interface is fired when the active history entry changes while the user navigates the session history. It changes the current history entry to that of the last page the user visited or, if history.

How do I stop Popstate?

related: call e. stopImmediatePropagation() to prevent subsequently-added popstate handlers from firing too. For instance, if a router listens to popstate and replaces content accordingly, you can prevent it from replacing content. The text you insist on re-adding isn't appropriate; stop rolling back.

What is Popstate event listener?

PopStateEvent is an interface for the popstate event. A popstate event is dispatched to the window every time the active history entry changes between two history entries for the same document. If the history entry being activated was created by a call to history. pushState() or was affected by a call to history.


1 Answers

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.

like image 167
James Kyburz Avatar answered Oct 21 '22 16:10

James Kyburz