faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

71 of 148 people (48%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

Is there any way to detect (react) from (within) HTML-code or JavaScript-Code when a user pressed the "Back"-button (to catch this event)?

Nov 22nd, 2008 17:48
haytham ali, ha mo, Manu G, Colin Fraser, Daniel Amsler, http://msryat.org


Sure, why not!
Use the onunload event of the body tag.
Something like this: 
<body OnUnload="alert('Woo hoo..');">
This will be called everytime the backbutton is pressed.. woo hoo.. :)
but one care needs to be taken.
This will also be called when any link or button on the screen are 
clicked. So be sure to apply some kinda check on the buttons/ links.
Something like:
<body onload="flag=true" onUnload="if (flag) alert ('Woo hooo..');">
.
.
<input type=button onclick="flag=false">
This validation will make sure that on the button click, that logic in 
the onunload thing is not called...
All the best & keep rockin'
~MeTaL~
http://6rb.com
http://fr7.com
http://dir.msryat.org
http://6ef.com
http://forums.msryat.org
http://www.arabcar.com
http://www.alshalah.com
http://msryat.org