Saturday, April 30, 2011

Detecting browser close event in Javascript

Below code is applicable for IE and Mozilla.


///Javascript starts here

var keyCd, altKy, mouseBtn, ctlKy, shiftKy;
Browser = navigator.appName
Net = Browser.indexOf("Netscape")
Micro = Browser.indexOf("Microsoft")
Netscape = false
IE = false
if (Net >= 0) {
    window.captureEvents(Event.MOUSEDOWN)
    window.onmousedown = HandleClose
    Netscape = true
}
if (Micro >= 0) {
    IE = true
}
function getKeyCode(event) {
    if (IE == true) {
        keyCd = event.keyCode;
        altKy = event.altKey;
        ctlKy = event.ctlKey;
        shiftKy = event.shiftKey;
    }
    if (Netscape == true) {
        if (event.which == null)
            keyCd = event.keyCode;
        else
            keyCd = event.which;
        altKy = event.altKey;
        ctlKy = event.ctrlKey;
        shiftKy = event.shiftKey;
    }
    if (keyCd == 13) {
        document.getElementById("ctl00_BtnLogout").disabled = true;
    }  
}
function getMouseButton(event) {
    if (IE == true) {
        mouseBtn = event.button;
    }
    if (Netscape == true) {
        if (event.which == null)
            mouseBtn = event.button;
        else
            mouseBtn = event.which;
       
    }
}
function HandleClose(e) {
    if (IE == true) {
        //Browser Closes through ALT + F4 buttons
        if (event.altKey == true && event.keyCode == 0) {
            PageMethods.DeleteSession();
        }
        //Browser Closes through Browser Close (X) button
        Xwidth = window.document.body.offsetWidth - window.event.clientX
        YHeight = window.event.clientY
        if (Xwidth <= 30 && YHeight < 0) {
            PageMethods.DeleteSession();
        }
        //Browser Closes through CTRL + F4 and CTRL + W buttons
        if (event.ctrlKey == true && event.keyCode == 0) {
            PageMethods.DeleteSession();
        }
        //Browser Closes through ALT + SPACE + C buttons
        if (keyCd == 32 && altKy == true) {
            PageMethods.DeleteSession();
        }
        //Browser Closes through Mouse right Click in Task bar
        if (mouseBtn == null && keyCd == null) {
            if (window.event.clientX > 1000) {
                PageMethods.DeleteSession();
            }
        }
    }
    else if (Netscape == true) {   
        //Browser Closes through ALT + F4 buttons
        if (altKy == true && keyCd == 115) {
            PageMethods.DeleteSession();
            alert('Hi1');
        }
     
        //Browser Closes through CTRL + F4 and CTRL + W buttons
        if (ctlKy == true && (keyCd == 87 || keyCd == 115)) {
            PageMethods.DeleteSession();
            alert('Hi2');
        }
        //Browser Closes through ALT + SPACE + C buttons
        if (altKy == true && keyCd == 18) {
            PageMethods.DeleteSession();
            alert('Hi3');
        }
       
        //Browser Closes through Mouse right Click in Task bar
        if (mouseBtn == null && keyCd == null) {
            PageMethods.DeleteSession();
            alert(e.X);
        }
    }
}

 ///Javascript ends here

///Html or Asp.net code starts here

<body onbeforeunload="HandleClose(event)" onKeyDown="return getKeyCode(event)" onMouseDown= "getMouseButton(event)">
///Your code here
</body>

///Html or Asp.net code ends here