preserving browser "back" button functionality using AJAX/jQuery to load pages and history.pushState() method

27,896

Solution 1

I'd like to direct everyone to a jQuery plugin called PJAX which accomplishes what I was ultimately interested in asking this question. Pjax() combines jQuery AJAX and pushState for page loading and preserving browser history.

Here's the code, and here's a nice demo of pjax() in action.

It is really great and easy to use, in the demo, just remember to click the checkbox which is there to help demonstrate pjax()'s functionality.

Solution 2

For this you need to subscribe to the 'popstate' event.

The popstate event is fired in certain cases when navigating to a session history entry. HTML5 spec

You could do this like this:

window.onpopstate = function() {
  $('#main_content').load(location.href)
};

Solution 3

You can use the popstate event for this purpose. See https://developer.mozilla.org/en/DOM/window.onpopstate for details.

Also have a look at History.js which provides a wrapper and can fallback to url hashtags in case the browser does not support the history api.

Share:
27,896
tim peterson
Author by

tim peterson

web programming-javascript, php, mysql, css, html-is my thang

Updated on July 22, 2022

Comments

  • tim peterson
    tim peterson almost 2 years

    I'd like to preserve the back button functionality when loading pages via AJAX (jQuery load method) and pushing the URL to the browser bar via the history.pushState method. The problem occurs when one clicks on the browser back button and the 1st click only restores the previous URL but does not load the previous page.

    Here's my code so far:

    $(function(){
      var profile_url= "/profile";
       $('#click_button').click(function(){
          $('#main_content').load(profile_url);
          history.pushState({profile:profile_info}, "profile", profile_url);
       });
    });
    

    My question is is there any way to do an AJAX load of the previous page into the #main_content div on the 1st click of the back button?

    Any help/advice would be greatly appreciated.

  • tim peterson
    tim peterson over 12 years
    awesome Anton, thanks that is exactly what i needed! Now i realize how about the browser "forward" button? Any way to control that with jQuery load() method? When i click the forward button after clicking the back button it, not surprisingly, doesn't re-load the page i just navigated away from, thanks, -tim
  • Anton Timmermans
    Anton Timmermans over 12 years
    popstate also fires when the forward button is pressed.
  • tim peterson
    tim peterson over 12 years
    hi Anton, how to discern then between the back and forward button? Is there some kind of if()/else you can do if the user clicks back vs. forward? thanks! -tim
  • Roland Burda
    Roland Burda over 11 years
    It's a great plugin for jQuery!! Thank you!!
  • hexacyanide
    hexacyanide about 11 years
    For anyone in the future who stumbles upon this, PJAX now has an event called pjax:popstate.
  • carefulnow1
    carefulnow1 about 7 years
    demo link broken