[RESOLVED]Simple JavaScript code not working

Hi everyone,

The following code is used to call the cleanUp.aspx when I close the web browser. It works for IE11. But the code does not work for FireFox, Opera, Chorme, and Safrai.

If possible, please modify my code below for making it works for FireFox, Opera, Chorme, and Safari web browsers.

Thank you,

May

<script language="javascript" type="text/javascript">

    var isClose = false;
   
    //this code will handle the F5 or Ctrl+F5 key
    //need to handle more cases like ctrl+R whose codes are not listed here
    document.onkeydown = checkKeycode
    function checkKeycode(e) {
        var keycode;
        if (window.event)
            keycode = window.event.keyCode;
        else if (e)
            keycode = e.which;
        if (keycode == 116) {
            isClose = true;
        }
    }
    function somefunction() {
        isClose = true;
    }

    //<![CDATA[

    function bodyUnload() {

        

        if (!isClose) {           

            var request = GetRequest();
            request.open("GET", "cleanUp.aspx", true);
            request.send();
        }
    }
    function GetRequest() {
        var request = null;
        if (window.XMLHttpRequest) {

            alert("Window Is Closing for IE7, FF, Opera, and Safari browser.");

            //incase of IE7,FF, Opera and Safari browser
            request = new XMLHttpRequest();
        }
        else {
            //for old browser like IE 6.x and IE 5.x

            alert("Window Is Closing for old browser like IE 6.x and IE 5.x");

            request = new ActiveXObject('MSXML2.XMLHTTP.3.0');
        }
        return request;
    }
    //]]>
</script>
<body style="margin:0 auto;width:100%; background-color:white;" onbeforeunload="bodyUnload();" onmousedown="somefunction()">

Hi,

try using onUnload event instead of onBeforeUnload (which might be generic to all browsers).

<body style="margin:0 auto;width:100%; background-color:white;" onunload="bodyUnload();" onmousedown="somefunction()">

Leave a Reply