Search code examples
javascriptjqueryajaxurl-rewritinghashchange

When using back button AJAX results have been lost


So I've set up a pagination system similar to Twitter's where where 20 results are shown and the user can click a link to show the next twenty or all results. The number of results shown can be controlled by a parameter at the end of the URL however, this isn't updated with AJAX so if the user clicks on one of the results and then chooses to go back they have to start back at only 20 results.

One thought I've had is if I update the URL when while I'm pulling in the results with AJAX it should—I hope—enable users to move back and forth without losing how many results are shown.

Is this actually possible or have I got things completely wrong?

Also, how would I go about changing the URL? I have a way to edit the URL with javascript and have it be a variable but I'm not sure how to apply that variable to the URL.

Any help here would be great!

A side note: I'm using jQuery's load() function to do all my AJAX.


Solution

  • You can't actually change the url of the page from javascript without reloading the page.

    You may wish to consider using cookies instead. By setting a client cookie you could "remember" how many results that user likes to see.

    A good page on javascript cookies.