Detect when user clicks the Back button

How do I do this in JavaScript?

It is not my intent to disable or interefere with this process.  In fact, it needs to work -- I just need to know when it happens is all.

Solution must work in IE and Mozilla, minimum.

Thanks!
LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tomarse111Commented:
There is no JS method of physically detecting when the back button has been pressed unfortunately. The only method I could think of is by using some sort of cookie method on subsequently pages and then detecting if they exist on pages prior to the cookie creation / modification.

Looking around I found this script which does this sort of thing. This is not fool proof though, for example, a page redisplay from cache uses the same measured events, in the same order, as a "back" button click.

Have a play with it and see if it helps.


<script type="text/javascript">
<!-- Copyright 2007 Bontrager Connection, LLC
// http://BontragerConnection.com/
 
// Cookie's name may be changed.
var CookieName = "tempFocus";
 
// Change number of seconds if needed.
var PageLoadTimeSeconds = 3;
 
// Replace alert(...) in function HandleNewFocus(), below, 
//    with your custom "'back' button was clicked" handling.
function HandleNewFocus()
{
alert('It seems the "back" button was clicked.');
}
 
// No customizations required in the rest of this JavaScript.
 
var now = new Date();
var Milliseconds = parseInt(now.getTime() / 1000);
var CurrentCookie = new String();
var FocusLaunched = false;
var BackHandled = false;
 
function HandleCookie()
{
if(BackHandled) { return; }
BackHandled = true;
var beforemillis = parseInt(CurrentCookie);
if(beforemillis > 1) {
   var aftermillis = Milliseconds - beforemillis;
   if(aftermillis > PageLoadTimeSeconds) { HandleNewFocus(); }
	}
else { SetCookie('loaded'); }
}
 
function LoadFunctions()
{
CurrentCookie = GetCookie();
if(CurrentCookie == 'loaded') { return; }
HandleCookie();
SetCookie('loaded');
}
 
function Loaded() { LoadFunctions(); }
 
function Unloaded() { 
var again = new Date();
Milliseconds = parseInt(again.getTime() / 1000);
SetCookie(String(Milliseconds));
}
 
function Focused() {
if(FocusLaunched) { return; }
FocusLaunched = true;
LoadFunctions();
}
 
function SetCookie(v) {
CurrentCookie = v;
document.cookie = CookieName + '=' + v;
}
 
function GetCookie() {
var c_content = '';
if(document.cookie.length > 0) {
   var c_name = CookieName + '=';
   var c_begin = document.cookie.indexOf(c_name);
   var c_end = 0;
   if(c_begin > -1) {
      c_begin += c_name.length;
      c_end = document.cookie.indexOf(";",c_begin);
      if(c_end < c_begin) { c_end = document.cookie.length; }
      c_content = document.cookie.substring(c_begin,c_end);
      }
   }
return c_content;
}
 
window.onload = Loaded;
window.onunload = Unloaded;
window.onfocus = Focused;
</script>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tom KnowltonWeb developerAuthor Commented:
What about hooking into IE or Mozilla?  Don't these browsers expose an API to javascript?
0
Tomarse111Commented:
As far as i'm aware you cannot Hook into IE or FF to control or detect the back button natively through JS. The cookie method i think is your best bet (although not perfect) but one of the experts might tell you differently :)
0
Tom KnowltonWeb developerAuthor Commented:
What about using Sessions?

Not to detect a back click, but to record where I  just was, and when I click the back button, where I am coming from to return to the last page I was on.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.