...

View Full Version : How do I generate an alert message when closing a browser?



Robb
01-01-2004, 11:56 PM
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.

Thanks,
ROBB

scroots
01-02-2004, 12:29 AM
have a look at
http://www.javascriptkit.com/javatutors/alert1.shtml

that site has loads of good scripts and tutorials, and is run by the person (George aka WA) who runs these forums.

scroots

fredmv
01-02-2004, 12:53 AM
IE-only:
<body onbeforeunload="return 'Are you sure?';">

Dingbat
01-02-2004, 10: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 :(

Code Wizard
01-02-2004, 02: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

nolachrymose
01-02-2004, 02: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! ;)

Code Wizard
01-02-2004, 02:43 PM
Oh...dear me.....:eek:

Skyzyx
01-02-2004, 03: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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum