How to fix the back button when user clicks a link

2019-04-16 10:10发布

问题:

I have infinite-scrolling pagination on a search result page using ajax.

How can I make it so that when a user clicks a link off-site and returns via the back button, the results are scrolled into the ajax-loaded portion of the page where they left?

回答1:

Google for jQuery ajax history plugins. There are many.

also look up jQuery haschange plugin: http://benalman.com/projects/jquery-hashchange-plugin/

basically you append / update (possibly increment) a '#result-set=1' to the page's URL. Then using the haschange plugin for eg. you can trigger your own code, move back or forward to how much of the results were shown by pagination etc. When a user clicks the back button and comes back to your page, the on load firing of haschange should work if you've wired up your pagination to work of the '#...' in the URL



回答2:

Check out unspace's writeup of their Pageless implementation. They use a special div which gets moved along with scroll events, and also they use a temporary cookie to store the position.