Call Javascript when browser close button is clicked

S

sreedhardasi

Hi,

I would like to call a javascript function when user clicks on
browser's close button. Here is the scenario.

1. User clicks browser close button.
2. User will be displayed a popup or a div with two buttons (Yes or
No) by calling a Javascript function
3. "No" will close the window.
4. "Yes" will cancel the user action and redirect him/her to different
page.

Is it possible to do this? I would appreciate if anyone help me with
this.

Thanks,
Sreedhar
 
V

VK

Hi,

I would like to call a javascript function when user clicks on
browser's close button. Here is the scenario.

1. User clicks browser close button.
2. User will be displayed a popup or a div with two buttons (Yes or
No) by calling a Javascript function
3. "No" will close the window.
4. "Yes" will cancel the user action and redirect him/her to different
page.

Is it possible to do this? I would appreciate if anyone help me with
this.

Yes, you can do it using onbeforeunload event listener.

function confirmUnload() {
var mes = "Your message here";
if (event) {
event.returnValue = mes;
}
else {
// do what?
}
}

window.onbeforeunload = confirmUnload;

The first branch is for the standard event handling. I know that
Firefox supports this as well by now but in some rather tweaky way and
I don't have the workaround snippet handy right now, so for // do
what? someone else may help here.
 
T

Thomas 'PointedEars' Lahn

VK said:
Yes, you can do it using onbeforeunload event listener.

But you should not, as it is an event handler, and the event applies to
navigating away as well. Besides, if there is no sufficient DOM support
you will have a problem.


PointedEars
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top