Click here to Skip to main content
15,069,075 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
i am wondering for Window Close Event of Browser that will be raised on browser close button only it should not fired during refreshing page.
i want only event will raise on close button of browse not any other case.
Thanks!!
Posted

This is a very common issue. So far it doesn't seem there is a perfect solution. This is the best I can do though:

Code
JavaScript
$(function () {
    $("a").click(function (evt) {
        confirm_leave = false;
    });
});
var confirm_leave = true;
window.onbeforeunload = function (evt) {
    if (confirm_leave === true) {
        var leave_message = "Are you sure you want to leave this page?";

        if (!evt) evt = window.event;

        evt.cancelBubble = true;
        evt.returnValue = leave_message;

        if (evt.stopPropagation) {
            evt.stopPropagation();
            evt.preventDefault();
        }

        return leave_message;
    } else {
        confirm_leave = true;
        /* ... handle unload without confirmation message ... */
    }
};


Explanation

First, if there are any links (can include buttons too) then this assigns a click event that sets a flag to disable the confirmation message.
JavaScript
$(function () {
    $("a").click(function (evt) {
        confirm_leave = false;
    });
});


Next, the default value of the flag is set to true (enable the confirmation message).
JavaScript
var confirm_leave = true;


Then the onbeforeunload event handler is set. The function checks the flag to see if the confirmation message should be shown.

Flag Is True
1. store the confirmation message to display.
2. get the appropriate event.
3. don't allow event bubbling/propagation.
4. set the event's returnValue to the confirmation message. (Browsers including IE)
5. return the confirmation message (Other browsers).
JavaScript
if (confirm_leave === true) {
    var leave_message = "Are you sure you want to leave this page?";

    if (!evt) evt = window.event;

    evt.cancelBubble = true;
    evt.returnValue = leave_message;

    if (evt.stopPropagation) {
        evt.stopPropagation();
        evt.preventDefault();
    }

    return leave_message;
}


Flag Is False
If the flag is not set to true, then set it to true. Since the flag has a default to true, then this flag must have been changed due to a link/button click.
JavaScript
else {
    confirm_leave = true;
    /* ... handle unload without confirmation message ... */
}


Problem

There is a potential problem with this solution though. If the user opens the link in a new window, the flag will be set to false but the onbeforeunload event handler will not be called (causing the flag to not be reset). Hopefully this will get things going for you though.
   
This event fires only on click of close button
XML
function handleWindowClose() {
          if ((window.event.clientX < 0) || (window.event.clientY < 0)) {
            
                  }
              }


call this script in body tag as follows

<body onbeforeunload="handleWindowClose();"
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900