Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting when the BACK button is clicked? Is it possible? 3

Status
Not open for further replies.

ChrisMacPherson

Programmer
Jul 3, 2000
157
0
0
GB
Can anybody tell me if it is possible to detect when the browsers back button is clicked?
Chris MacPherson
thedamager@hotmail.com
Bring on the new Browza's!!

Learn/t/ing D\HTML, Javascript, Java, VB5-6, COBOL, Pascal
 
it is not possible ... but u can write some server side logic to handle it

- ayyappan.
 
Thanks, but for this site I have no server access! Nevermind it's not too big a problem, I KNOW there are other ways around it, it's just finding them.
Chris MacPherson
thedamager@hotmail.com
Bring on the new Browza's!!

Learn/t/ing D\HTML, Javascript, Java, VB5-6, COBOL, Pascal
 
With HTML use onUnload="" in the Body tag

EXAMPLE

<Body onBodyUnload=&quot;alert('Bye')&quot;>

>:):O>
 
OnUnload is for whenever the document closes, not just when the back button is hit. That event occurs when you click any link or just close the browser.

To detect the back button specifically is quite difficult. What you might be able to do is pass a query string parameter which indicates the age of the page. For instance, pass the time on the query string, and if the query string time is older than, say, one minute, then you know the back button was pressed rather than a new link to that page.

You might also be able to do something with the history object. For instance, if you call onload=history.forward() every time the page loads, then you will always remain at the very end of your history list. Clicking the back button will cause you to have a forward history, so the page will take you back to where you were before clicking the back button. However, if it is the first time you are at that page, then you will already be at the end of your history list, so it won't take you anywhere. Fortunately, it does not return an error if you are at the end of your history list either... it just stays at that page.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
I am looking for a similar solution i.e. to trap the back button press.

I didn't understand this part

&quot;To detect the back button specifically is quite difficult. What you might be able to do is pass a query string parameter which indicates the age of the page. For instance, pass the time on the query string, and if the query string time is older than, say, one minute, then you know the back button was pressed rather than a new link to that page.&quot;

Can you please explain to me how this can be done? If you can attach the script then it will be great.

Thanks

Ketan
 
Ok, imagine that some page on your site is


If you include a timestamp in all of your links, eg.

<script>
time = new Date(); // get current time
document.write('<a href=&quot; time+'>some page</a>');
</script>

then you can compare the passed-in time with the current time, and if larger than some predefined time limit, do whatever you need to do when the back button is pressed, eg.

<script>
var t; var re;
t = &quot;&quot;+document.location+&quot;&quot;; // url passed in
re = /(.*?)t=/i; // make a regular expression matching until the equal sign
t = t.replace(re, &quot;&quot;); // replace it with nothing, leaving the time remaining
if ((time - t)>10000) // if page is more than 10 sec old
{ do_something(); } // the back button was pressed; do something
</script>

That should be sufficient for your needs. BTW, doing a non-greedy match by using the ? breaks the script in IE 5.0. That is a bug in the browser. Updating to IE 5.5 fixes that bug.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Thank you very much, Tom.

I will try this script and let you know if I have any problem.

Ketan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top