Call Javascript when browser close button is clicked

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sreedhardasi@gmail.com

    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
  • VK

    #2
    Re: Call Javascript when browser close button is clicked

    On Mar 4, 10:14 pm, sreedhard...@gm ail.com wrote:
    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.returnVal ue = mes;
    }
    else {
    // do what?
    }
    }

    window.onbefore unload = 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.

    Comment

    Working...