Jquery when back button is pressed

user2568107 picture user2568107 · Jul 13, 2013 · Viewed 58.8k times · Source

I have a click that changes things on the page when you click it. I was wondering if there was a way to trigger an effect when you press the back button on the browser

Answer

user1721135 picture user1721135 · Jul 13, 2013

You can sort of do it using the popstate event:

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

or in jQuery:

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

However this will also trigger when navigating forward, not only backward.