JQuery when clicking back button

I have a click that changes objects on the page when I click on it. I was wondering if there is a way to cause an effect when you click the back button in the browser

+7
jquery browser back
source share
4 answers

You can do this using the popstate event:

window.onpopstate = function() { alert("pop!"); } 

or jQuery :

 $(window).on('popstate', function(event) { alert("pop"); }); 

However, this also works when navigating forward, not just backward.

+11
source share

Disable return url

 $(function() { if (window.history && window.history.pushState) { window.history.pushState('', null, './'); $(window).on('popstate', function() { // alert('Back button was pressed.'); document.location.href = '#'; }); } }); 
+3
source share

You can simply fire the "popState" event in jQuery, for example:

 $(window).on('popstate', function(event) { alert("pop"); }); 

This is enough for what you requested ... but here, add a little window to the event to go from ...

 $(window).on('pushstate', function(event) { alert("push"); }); // This one pushes u to forward page through history... window.history.replaceState(); //work exactly like pushstate bt this one replace the history instead of creating new one... window.history.backward(); // To move backward through history, just fire this event... window.history.forward();// To move forward through history ... window.history.go(-1); // Go back to one page(or whatever value u passed) through history window.history.go(1); // Go forward to one page through history.. 
0
source share

The best way is to set the timeout using jquery.

 function explode(){ alert("YOUR EVENT HERE!"); } setTimeout(explode, 1); 

In milliseconds, it will run the function once, so your users may not even notice its operation.

I use it without a problem.

-3
source share

All Articles