Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does popstate fire?

I put all of my code in $(document).ready as per norm, but should I put my 'popstate' listener at the very end of this code too? Or does it matter?

like image 401
Matthew Avatar asked May 25 '11 18:05

Matthew


People also ask

How do you trigger a Popstate?

The popstate event will be triggered by doing a browser action such as a click on the back or forward button (or calling history. back() or history. forward() in JavaScript). Browsers tend to handle the popstate event differently on page load.

How do I cancel Popstate?

This is all quite simple. What you have to do is to make hash not to get added to the browser address bar. For this basically you have to add click event handler, in there check if the link is the one you want to avoid firing popstate on hash change and, if yes, prevent default behaviour with event. preventDefault() .


3 Answers

This doesn't really matter, and since it's an event, can even be done before your ready method. The only thing needing to be placed inside document ready is code interacting with the DOM. Everything else doesn't (and possibly shouldn't) be placed in document ready.

Example:

window.onpopstate = function() {
    // binding this event can be done anywhere, 
    // but shouldn't be inside document ready
};

$(document).ready(function() {
    // DOM manipulation and other stuff
});

Now when popstate actually is triggered is a lot different than when it is bound. According to the Mozilla doc:

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 173
Eli Avatar answered Oct 17 '22 15:10

Eli


I was confused, because popstate was firing on every page load (I use chrome).

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

Browsers tend to handle the popstate event differently on page load. Chrome and Safari always emit a popstate event on page load, but Firefox doesn't.

So in chrome (and apparently safari), the jquery ready method will execute, followed by the popstate event. i.e. it doesn't matter if you attach the event within ready (or body.onload for that matter), the popstate event will happen after your ready method is complete.

In firefox (16.0.1) the popstate event does not fire on page loads (I can't test IE 10).

like image 44
zod Avatar answered Oct 17 '22 17:10

zod


https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onpopstate

Calling history.pushState() or history.replaceState() won't trigger a popstate event. The popstate event is only triggered by performing a browser action, such as clicking on the back button (or calling history.back() in JavaScript), when navigating between two history entries for the same document.

like image 1
Farhad Azarbarzinniaz Avatar answered Oct 17 '22 16:10

Farhad Azarbarzinniaz