Connecting Tech Pros Worldwide Help | Site Map

Call Javascript when browser close button is clicked

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 4th, 2008, 06:15 PM
sreedhardasi@gmail.com
Guest
 
Posts: n/a
Default Call Javascript when browser close button is clicked

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, 07:05 PM
VK
Guest
 
Posts: n/a
Default 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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.