Track when a user clicks a button in a browser

Is it possible to determine when the user clicks the browser button?

I have an Ajax application, and if I can detect when the user clicks the back button, I can display the relevant data

Any solution using PHP, JavaScript is preferable. A hell of a solution in any language is fine, you just need something that I can translate into PHP / JavaScript

Edit: cut and paste from below:

Wow, all great answers. I would like to use Yahoo, but I already use the Prototype and Scriptaculous libraries and do not want to add more ajax libraries. But it uses iFrames, which gives me a good pointer to my own code.

+22
javascript browser php
Sep 11 '08 at 5:52
source share
7 answers

There are several ways to do this, although some of them will only work in certain browsers. The one I know from the start is to insert a tiny invisible iframe onto the page. When the user clicks the back button, the iframe moves back, which you can detect and then refresh your page. Here is another solution.

You may also want to look at the source on something like gmail and see how they do it.

Here is the library for the thing you are looking for, by the way

+13
Sep 11 '08 at 6:01
source share

One of my favorite frameworks for this is Yahoo! Browser History Manager . You register events and forward them when the user returns Back to this state. And if you want to know how it works, here's an interesting blog post about Yahoo! solutions made during its development.

+15
Sep 11 '08 at 6:10
source share

It is impossible to say when the user presses the back button, presses the back button to return to the browser, however there are other events that occur in a certain order that can be detected. This javascript example has a good enough method for detecting reverse commands:

The traditional way is to track the movement of the user through your site using cookies or links. When the user goes to page A, then to page B, appears again on page A (especially when there is no link to B on A), then you know that they are back - A can detect this and redirect them one way or another.

+6
Sep 11 '08 at 6:04
source share

Yahoo's user interface library, my personal favorite JS client library, has an excellent โ€œBrowser History Managerโ€ that does exactly what to ask.

+3
Sep 11 '08 at 6:14
source share

The dojo toolkit has functionality to handle this in javascript. I don't think there is a good way to handle this in pure PHP.

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

+1
Sep 15 '08 at 20:16
source share

I set the $wasPosted variable to $_SESSION with false .

All my messages go through the same php file and set $wasPosted to true .

All header(location:) requests precede setting $wasPosted to true .

If $wasPosted is false , then the page was loaded after using the back or forward buttons.

+1
Dec 16 '15 at 10:09
source share

The easiest way to check if you are back in the cached version of your page that you need to refresh is to add a hidden input element that will be cached, and you can check if it has a default value.

Just put the following in your body tag. I put it right in front of 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> 
0
Oct 30 '15 at 15:28
source share



All Articles