Connecting Tech Pros Worldwide Help | Site Map

Call Javascript when browser close button is clicked

sreedhardasi@gmail.com
Guest
 
Posts: n/a
#1: Mar 4 '08
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
VK
Guest
 
Posts: n/a
#2: Mar 4 '08

re: Call Javascript when browser close button is clicked


On Mar 4, 10:14 pm, sreedhard...@gmail.com wrote:
Quote:
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.
Closed Thread