Capture event when browser is closing

D

Derek

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.
 
M

Martin Honnen

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;">
 
D

DU

Derek said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top