<p>Is it possible to detect when the user clicks on the browser's back button?</p>
<p>I have an Ajax application and if I can detect when the user clicks on the back button I can display the appropriate data back</p>
<p>Any solution using PHP, JavaScript is preferable. Hell a solution in any language is fine, just need something that I can translate to PHP/JavaScript</p>
<h3>Edit: Cut and paste from below:</h3>
<p>Wow, all excellent answers. I'd like to use Yahoo but I already use Prototype and Scriptaculous libraries and don't want to add more ajax libraries. But it uses <em>iFrames</em> which gives me a good pointer to write my own code.</p>

[{'answer_id': 55880, 'author': 'George Mauer', 'author_id': 5056, 'author_profile': 'https://Stackoverflow.com/users/5056', 'pm_score': 5, 'selected': True, 'text': '<p>There are multiple ways of doing it, though some will only work in certain browsers.  One that I know off the top of my head is to embed a tiny near-invisible iframe on the page.  When the user hits the back button the iframe is navigated back which you can detect and then update your page.  <a href="http://www.ajaxonomy.com/2007/web-design/a-better-ajax-back-button-solution" rel="noreferrer">Here</a> is another solution.</p>\n\n<p>You might also want to go view source on something like gmail and see how they do it.</p>\n\n<p><a href="http://www.codeproject.com/KB/ajax/Salajax.aspx" rel="noreferrer">Here\'s</a> a library for the sort of thing you\'re looking for by the way</p>\n'}, {'answer_id': 55883, 'author': 'Adam Davis', 'author_id': 2915, 'author_profile': 'https://Stackoverflow.com/users/2915', 'pm_score': 3, 'selected': False, 'text': '<p>There\'s no way to tell when a user clicks the back button of presses the backspace key to go back in the browser, however there are other events that happen in a certain order which are detectable.  This <a href="https://web.archive.org/web/20080214125014/http://www.willmaster.com/library/javascript/detecting_a_back_button_click.php" rel="nofollow noreferrer">example javascript</a> has a reasonably good method for detecting back commands:</p>\n\n<p>The traditional way, however, is to track user movement through your site using cookies or referrer pages.  When the user goes to page A, then page B, then appears at page A again (especially when there\'s no link on B to A) then you know they went back - A can detect this and redirect them or otherwise.</p>\n'}, {'answer_id': 55886, 'author': 'Wayne Kao', 'author_id': 3284, 'author_profile': 'https://Stackoverflow.com/users/3284', 'pm_score': 4, 'selected': False, 'text': '<p>One of my favorite frameworks for doing this is <a href="http://developer.yahoo.com/yui/history/" rel="noreferrer">Yahoo!\'s Browser History Manager</a>.  You register events and it calls you back when the user returns Back to that state.  And if you want to learn how it works, <a href="http://yuiblog.com/blog/2007/02/21/browser-history-manager/" rel="noreferrer">here\'s a fun blog entry about the decisions Yahoo! made</a> when designing it.</p>\n'}, {'answer_id': 55888, 'author': 'Josh Hinman', 'author_id': 2527, 'author_profile': 'https://Stackoverflow.com/users/2527', 'pm_score': 2, 'selected': False, 'text': '<p>The Yahoo User Interface Library, my personal favorite client-side JS library, has an excellent <a href="http://developer.yahoo.com/yui/history/" rel="nofollow noreferrer">Browser History Manager</a> that does exactly what you\'re asking for.</p>\n'}, {'answer_id': 66503, 'author': 'Scott Swezey', 'author_id': 9439, 'author_profile': 'https://Stackoverflow.com/users/9439', 'pm_score': 1, 'selected': False, 'text': '<p>The dojo toolkit has functionality to deal with this in javascript. I don\'t think there is any good way to handle it in pure PHP.</p>\n\n<p>Here is the docs page they have: <a href="http://dojotoolkit.org/book/dojo-book-0-9/part-3-programmatic-dijit-and-dojo/back-button-undo" rel="nofollow noreferrer">http://dojotoolkit.org/book/dojo-book-0-9/part-3-programmatic-dijit-and-dojo/back-button-undo</a></p>\n'}, {'answer_id': 33439550, 'author': 'Jeffrey Harmon', 'author_id': 555329, 'author_profile': 'https://Stackoverflow.com/users/555329', 'pm_score': 2, 'selected': False, 'text': '<p>The simplest way to check if you came back to a cached version of your page, which needs to be refreshed, is to add a hidden input element that will be cached, and you can check if it still has its default value.</p>\n\n<p>Just place the following inside your body tag.  I place mine right before the end tag.</p>\n\n<pre><code>&lt;input type="hidden" id="needs-refresh" value="no"&gt;\n&lt;script&gt;\n    onload=function(){\n        var e = document.getElementById("needs-refresh");\n        if (e.value === "yes")\n            location.reload();\n        e.value = "yes";\n    }\n&lt;/script&gt;\n</code></pre>\n'}, {'answer_id': 34309128, 'author': 'Drew Shardlow', 'author_id': 5686050, 'author_profile': 'https://Stackoverflow.com/users/5686050', 'pm_score': 2, 'selected': False, 'text': '<p>I set a variable <code>$wasPosted</code> in <code>$_SESSION</code> with value <code>false</code>.</p>\n\n<p>All my posts go via the same php file, and set <code>$wasPosted</code> to <code>true</code>.</p>\n\n<p>All <code>header(location:)</code> requests are preceded by setting <code>$wasPosted</code> to <code>true</code>.</p>\n\n<p>If <code>$wasPosted</code> is <code>false</code> then the page was loaded after use of the backward or forward buttons.</p>\n'}]
['https://Stackoverflow.com/questions/55871', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3983/']