Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
18
rated 0 times [  19] [ 1]  / answers: 1 / hits: 28117  / 13 Years ago, fri, october 14, 2011, 12:00:00

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.


More From » ajax

 Answers
31

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.


[#89610] Thursday, October 13, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
denis

Total Points: 260
Total Questions: 87
Total Answers: 87

Location: Venezuela
Member since Thu, Jul 15, 2021
3 Years ago
;