Search code examples
jquerybrowserback

Jquery when back button is pressed


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


Solution

  • 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.