<p>I am familiar with several approaches to making the back button work in AJAX applications in various situations, but I have not found a solution that will work gracefully in my specific scenario.</p>

<p>The pages I am working with are the search interface for a site.  You enter terms in a normal search box, click "go and wind up at a search results page.  On the search results page there are a ton of UI controls for filtering/sorting the search results to find what you are looking for.  Some of the operations triggered by these controls may take a (relatively) long time to complete (e.g. several seconds).</p>

<p>This latency is fine in case where the user is initially filtering/sorting their results... there's a nice AJAX spinner and so on... however when the user clicks on a search result and then clicks on the BACK button, I would like the page to instantly be restored to the state it was in when they clicked through.  </p>

<p>I can restore the states using IFRAMEs/fragment identifiers as a dictionary of page history, but what ends up happening is that when the user first hits the back button the initial page is loaded, then it (re) makes the AJAX query to get the page state back, which triggers the AJAX spinner and another wait of possible several seconds.</p>

<p>Is there any approach that does not require this kind of two-stage load of the page when the user returns to the page via the BACK button?</p>

<p>Edited to add: I am partial to jquery but I'd be happy with solutions that depend on other libraries/toolkits or that are standalone/raw javascript.</p>

<p>Edited to add: I should've added that I'm trying to avoid cookies/sessions because this prevents people having multiple brower windows/tabs open and manipulating different sets of search results at the same time.</p>

<p>Edit: Matt, can you elaborate on your proposed solution (triggering a page change event via fragment identifer)? I see how this would help with BACK button clicks across the same page but not coming BACK to the search results page after clicking on a specific result.</p>

[{'answer_id': 34242, 'author': 'Avner', 'author_id': 1476, 'author_profile': 'https://Stackoverflow.com/users/1476', 'pm_score': 1, 'selected': False, 'text': '<p>Just use a <strong>cookie</strong>.</p>\n'}, {'answer_id': 34259, 'author': 'Matt Sheppard', 'author_id': 797, 'author_profile': 'https://Stackoverflow.com/users/797', 'pm_score': 0, 'selected': False, 'text': '<p>Would it help to trigger a page change event using the "Add some info to the # at the end of the URL approach".</p>\n\n<p>That way, clicking the back button shouldn\'t actually change the page, and you should be able to restore state without the first page load.</p>\n'}, {'answer_id': 34283, 'author': 'paan', 'author_id': 2976, 'author_profile': 'https://Stackoverflow.com/users/2976', 'pm_score': 0, 'selected': False, 'text': "<p>Use something persistent that is tied to the user's profile.  </p>\n\n<p>Cookies and sessions are good ideas, but you can also keep those stuff in the database. That gives you an added advantage of being able to save the user's filtering preferences accross different browsing session.(if, for exampple, he was looking for something in the office and then decided to continue searching when he is back at home).  </p>\n\n<p>It all depends on the complexity of the filters and weather or not it is something you think that the user will want to use accross diffrent browsing sessions.. </p>\n"}, {'answer_id': 271114, 'author': 'J.C. Inacio', 'author_id': 35292, 'author_profile': 'https://Stackoverflow.com/users/35292', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>Edited to add: I should\'ve added that\n  I\'m trying to avoid cookies/sessions\n  because this prevents people having\n  multiple brower windows/tabs open and\n  manipulating different sets of search\n  results at the same time.</p>\n</blockquote>\n\n<p>You can create a random token and assign it to the fragment identifier.</p>\n\n<ul>\n<li>on first page load create a token if no fragment identifier is set</li>\n<li>before navigating out, store all the temporary ajax data in a cookie with that token as index. </li>\n<li>when hitting back, if you have a fragment identifier set, load the data from the corresponding token in the cookie.</li>\n<li>you can even add a "time" field to expire tokens, etc...</li>\n</ul>\n\n<p>sample cookie (JSON):</p>\n\n<pre><code>{"ajaxcache":[{"token":&lt;token&gt;,"time":&lt;time&gt;,"data":&lt;data&gt;}, ... ]}\n</code></pre>\n'}, {'answer_id': 326295, 'author': 'Kent Brewster', 'author_id': 1151280, 'author_profile': 'https://Stackoverflow.com/users/1151280', 'pm_score': 1, 'selected': False, 'text': '<p>Have you investigated the <a href="http://developer.yahoo.com/yui/history/" rel="nofollow noreferrer">YUI Browser History Manager</a>? </p>\n'}, {'answer_id': 10234147, 'author': 'Nikita Koksharov', 'author_id': 764206, 'author_profile': 'https://Stackoverflow.com/users/764206', 'pm_score': 1, 'selected': True, 'text': '<p>Try to use <strong>localStorage</strong> object. Here is crossbrower libs <a href="http://www.jstorage.info/" rel="nofollow">jStorage</a> and <a href="http://afarkas.github.com/webshim/demos/demos/json-storage.html" rel="nofollow">WEBSHIMS json-storage</a></p>\n'}]
['https://Stackoverflow.com/questions/34227', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2168/']