By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,571 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,571 IT Pros & Developers. It's quick & easy.

Capture event when browser is closing

P: n/a
Hello:

I want to capture the event when a browser is closing, to give to the user
the posibility of close or no this browser. When the browser is closing,
this show a confirm window with two buttons: Accept and Cancel.

When press the Accept button, the browser is closing, and when press the
Cancel button, the browser isnīt closing.

I have tried with the event onunload, but this closes first the browser, and
then shows the confirm window.

Can somebody help me?. Thanks.
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a


Derek wrote:

I want to capture the event when a browser is closing, to give to the user
the posibility of close or no this browser. When the browser is closing,
this show a confirm window with two buttons: Accept and Cancel.

When press the Accept button, the browser is closing, and when press the
Cancel button, the browser isnīt closing.

I have tried with the event onunload, but this closes first the browser, and
then shows the confirm window.


IE and recent Mozilla versions implement
<body onbeforeunload="event.returnValue = 'Are you sure you want to
leave?'; return event.returnValue;">

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #2

P: n/a
DU
Derek wrote:
Hello:

I want to capture the event when a browser is closing, to give to the user
the posibility of close or no this browser. When the browser is closing,
this show a confirm window with two buttons: Accept and Cancel.

When press the Accept button, the browser is closing, and when press the
Cancel button, the browser isnīt closing.

I have tried with the event onunload, but this closes first the browser, and
then shows the confirm window.

Can somebody help me?. Thanks.


Put this in the <head> part of your html file:

<script type="text/javascript">
function init()
{
if(window.addEventListener)
{
window.addEventListener("beforeunload", unloadMess, false);
}
else if(window.onbeforeunload)
{
window.onbeforeunload = unloadMess;
};
}

function unloadMess()
{
var User_Message = "[Your user message here]"
return User_Message;
}
</script>
....
</head>
<body onload="init();" ...>

Note that both Mozilla and MSIE have a default question with Ok and
cancel buttons. You can not modify that part of the modal window.

DU
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.