Connecting Tech Pros Worldwide Help | Site Map

Call Javascript when browser close button is clicked

  #1  
Old March 4th, 2008, 07:15 PM
sreedhardasi@gmail.com
Guest
 
Posts: n/a
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
  #2  
Old March 4th, 2008, 08:05 PM
VK
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
FireFox (version 2) Ajax call to update page times out if child window is closed Frinavale answers 29 March 13th, 2009 07:21 PM
Detecting browser close event using Jscript or php somaskarthic answers 3 August 26th, 2006 12:56 AM