View Full Version : How do I generate an alert message when closing a browser?
I can't figure this one out, so I need so help.
I would like to have a "Are you sure?" alert box come up when the user tries to close their browser by clicking on the X or File/Close. The alert box needs to have 'yes' 'no' buttons. If they click 'no', the browser session doesn't close. I can get the alert box with unOnload, but the browser closes anyway.
01-02-2004, 01:29 AM
have a look at
that site has loads of good scripts and tutorials, and is run by the person (George aka WA) who runs these forums.
01-02-2004, 01:53 AM
<body onbeforeunload="return 'Are you sure?';">
01-02-2004, 11:27 AM
The tag "onbeforeunload" works by adding the text to the standard window close awrning popup.
That leads to a further question. Can the Cancel button on the popup beroved forcing the user to answer "yes"?
The Dingbat :(
01-02-2004, 03:06 PM
First of all I don't understand why do u use onbeforeunload rather than onunload.....
Second....this is how it's done:
<body onunload="return confirm('Are u sure?')">
And as they say...happy coding :p
01-02-2004, 03:13 PM
With onunload, the "unloading" of the page has already occurred, so returning false won't stop anything. That's why one must use onbeforeunload, as this event occurs *before* the actual unloading from the document happens.
Also, the onbeforeunload event is different since it doesn't act like all the rest in the sense that returning a value is actually a part of a confirm asking whether or not the user wishes to unload the document. Returning the value of confirm() will actually call two confirms: the first one being the one defined by the scripter, and the second will say something like:
Are you sure you want to navigate away from this page?
(return value of confirm)
Press OK to continue, or Cancel to stay on the page.
Happy coding! ;)
01-02-2004, 03:43 PM
01-02-2004, 04:04 PM
Of course, this will also come into play when the user tries to navigate away from the page... as well as when it's being closed.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.