Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 9-Nov-12 21:18pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
$(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.
$(function () {
    $("a").click(function (evt) {
        confirm_leave = false;
    });
});
 
Next, the default value of the flag is set to true (enable the confirmation message).
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).
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.
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This event fires only on click of close button
function handleWindowClose() {
          if ((window.event.clientX < 0) || (window.event.clientY < 0)) {
            
                  }
              }
 
call this script in body tag as follows
 
<body onbeforeunload="handleWindowClose();"
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 420
1 OriginalGriff 210
2 Kornfeld Eliyahu Peter 210
3 DamithSL 210
4 mukesh mourya 85
0 OriginalGriff 7,215
1 DamithSL 5,199
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,757
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 15 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100