Solved

Show alert when back button is pressed

Posted on 2006-11-22
9
3,269 Views
Last Modified: 2011-10-03
Hi all,

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

Thanks
0
Comment
Question by:zattz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 3

Expert Comment

by:viveksg
ID: 18001013
<script>
 window.close = function(){alert("your message");}
</script>
0
 
LVL 6

Expert Comment

by:aescnt
ID: 18001050
Don't you mean window.onunload?
Although, even clicking a link or going forward will make the event fire.
0
 

Author Comment

by:zattz
ID: 18001057
Dosn't work
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

by:
aescnt earned 250 total points
ID: 18001073
<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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 18001339
> .. show a message box when the back button is pressed?
impossible (except patching the browser)
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 250 total points
ID: 18001352
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 18002598
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
 
LVL 6

Expert Comment

by:aescnt
ID: 18002625
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
 

Author Comment

by:zattz
ID: 18002652
Hmm..

that window.location.hash is interesting,

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

thanks guys
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hta File displays dynamic File names 4 65
Horizontal Full Calendar using php 5 44
display data from multiple ajax calls 9 43
Sticking text to shapes in Raphael.js 6 41
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question