Link to home
Start Free TrialLog in
Avatar of zattz
zattz

asked on

Show alert when back button is pressed

Hi all,

how can I show a message box when the back button is pressed?

Thanks
Avatar of viveksg
viveksg

<script>
 window.close = function(){alert("your message");}
</script>
Don't you mean window.onunload?
Although, even clicking a link or going forward will make the event fire.
Avatar of zattz

ASKER

Dosn't work
ASKER CERTIFIED SOLUTION
Avatar of aescnt
aescnt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> .. show a message box when the back button is pressed?
impossible (except patching the browser)
SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
window.onunload() does not work if you navigate with the browser's back button from http://..../this#that to http://..../this

Hence this is not a working answer if you read the question verbatim.
Just my 2 pence ...
Hehe, yep... it's really not the fix for that. In fact, onunload is a very unreliable way to detect back navigations, I admit.
If you want to detect back navigations from this.htm#that to this.htm, I'd put a timer that checks window.location.hash -- I believe this method is being used by a few Flash-based websites to detect back button clicks.
Avatar of zattz

ASKER

Hmm..

that window.location.hash is interesting,

but anyway the onunload is good enough for the page I want it on.

thanks guys