开发者

Browser Back Button Ajax

开发者 https://www.devze.com 2023-02-18 04:44 出处:网络
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 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 happe开发者_StackOverflow社区n?


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消