Question

Is it possible to detect when the user clicks on the browser's back button?

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

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

Edit: Cut and paste from below:

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 iFrames which gives me a good pointer to write my own code.

Was it helpful?

Solution

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. Here is another solution.

You might also want to go view source on something like gmail and see how they do it.

Here's a library for the sort of thing you're looking for by the way

OTHER TIPS

One of my favorite frameworks for doing this is Yahoo!'s Browser History Manager. 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, here's a fun blog entry about the decisions Yahoo! made when designing it.

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 example javascript has a reasonably good method for detecting back commands:

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.

The Yahoo User Interface Library, my personal favorite client-side JS library, has an excellent Browser History Manager that does exactly what you're asking for.

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.

Just place the following inside your body tag. I place mine right before the end tag.

<input type="hidden" id="needs-refresh" value="no">
<script>
    onload=function(){
        var e = document.getElementById("needs-refresh");
        if (e.value === "yes")
            location.reload();
        e.value = "yes";
    }
</script>

I set a variable $wasPosted in $_SESSION with value false.

All my posts go via the same php file, and set $wasPosted to true.

All header(location:) requests are preceded by setting $wasPosted to true.

If $wasPosted is false then the page was loaded after use of the backward or forward buttons.

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.

Here is the docs page they have: http://dojotoolkit.org/book/dojo-book-0-9/part-3-programmatic-dijit-and-dojo/back-button-undo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top