Chris Muench
Chris Muench

Reputation: 18338

Browser Back Button Ajax

I know there a bunch of topics on this, but I couldn't determine what to do based on what I read in the other topics.

I have a page "abc.php". The user can do a search which then populates a form with 2 ajax requests. Then if the user navigates to another page and then clicks BACK to "abc.php", the contents of the form is not complete because the ajax doesn't run. Is there a way to make this happen?

Upvotes: 4

Views: 1638

Answers (3)

OneThreeSeven
OneThreeSeven

Reputation: 422

Using history.js, the following function 'listens' to changes in the url bar, and calls a function to load the appropriate page:

History.Adapter.bind(window,'statechange',function(){  
    var State = History.getState();  
    page(State.url);  
});

function page(url) {
    //AJAX
}

Now whenever you want to change the page, you call:

History.pushState({state:X}, "Page Title", "Page Url");

This will update the browser's url bar, and automatically call page(State.url) for the new url; and all the browser features like forward/back button, bookmarks, etc... should work.

Upvotes: 1

jeroen
jeroen

Reputation: 91792

This is a very nice article / tutorial on enabling the back-button using jQuery.

Upvotes: 1

jho
jho

Reputation: 2263

Modify your URL when you do the ajax by adding the search terms there after a hash (e.g. http://example.com/search.php#search-terms-here)

Then when the page is loaded, read the search terms back from the URL.

Upvotes: 4

Related Questions