Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Track when user hits back button on the browser

Tags:

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.

like image 911
Rushi Avatar asked Sep 11 '08 05:09

Rushi


People also ask

What happens when browser Back button is pressed?

A web page automatically forwarded you to an alternate page Sometimes, when you visit a web page, it automatically directs you to another page. If this occurs and you press the back button, you will immediately be re-forwarded to that same page again.

Can we control browser back button?

You can-not actually disable the browser back button. However, you can do magic using your logic to prevent the user from navigating back which will create an impression like it is disabled.


2 Answers

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.

like image 161
Wayne Kao Avatar answered Sep 22 '22 07:09

Wayne Kao


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

like image 38
George Mauer Avatar answered Sep 22 '22 07:09

George Mauer