Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3279
  • Last Modified:

Show alert when back button is pressed

Hi all,

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

Thanks
0
zattz
Asked:
zattz
  • 3
  • 2
  • 2
  • +2
2 Solutions
 
viveksgCommented:
<script>
 window.close = function(){alert("your message");}
</script>
0
 
aescntCommented:
Don't you mean window.onunload?
Although, even clicking a link or going forward will make the event fire.
0
 
zattzAuthor Commented:
Dosn't work
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
aescntCommented:
<html>
<head>
<script type="text/javascript">
window.onunload = function() { alert("You navigated out of the page."); }
</script>
</head>
<body>Try hitting the back button or something.</body>
</html>

IE and Firefox tested.
0
 
ahoffmannCommented:
> .. show a message box when the back button is pressed?
impossible (except patching the browser)
0
 
Michel PlungjanIT ExpertCommented:
As you said yourself:
"Try hitting the back button or something" where "something" is also "clicking a link or going forward"

This is such a FAQ.

zattz, please tell us why you need this functionality and perhaps we can give you a solution that does no require this alert.

For example to not HAVE a page to go back to, you can have a link on the page before:
<a href="page2.html" onClick="location.replace(this.href); return false">Replace this page with page 2</a>

Also you can do some things with framesets to avoid the alert to be triggered when moving back, and you can use this to stop links from triggering:

<body onLoad="leaving=false" onUnload="if (leaving) alert('Sorry to see you go')">
<a href="http://www.google.com/search?q=back+button+javascript" onClick="leaving=true">Leave for Google</a>
<a href="page2.html" >Staying on the site</a>
0
 
ahoffmannCommented:
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 ...
0
 
aescntCommented:
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.
0
 
zattzAuthor Commented:
Hmm..

that window.location.hash is interesting,

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

thanks guys
0
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.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now