js捕捉浏览器关闭事件onbeforeunload

<script type="text/javascript">
var READYTOPROCESS = false;
window.onbeforeunload = function closeWindow(e)
{
    if (!READYTOPROCESS)
    {
        //message to be returned to the popup box.
        var message = '你确定要关闭吗?',
            e = e || window.event;
        if (e)
            e.returnValue = message; // IE
        return message; // Safari
    }
};
</script>
<body onbeforeunload="checkLeave()">

 <script>
    function checkLeave(){
        alert("确认退出??")
        asdasd();
        event.returnValue="确定离开当前页面吗?";
    }
    function asdasd(){
        alert("asfsaf");
        xmlhttp = null;
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                alert(xmlhttp.responseText)
            }
        }
        xmlhttp.open("post", "loginOut");
        xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded")
        xmlhttp.send("username=011011&password=12511");
    }
</script>
</body>

猜你喜欢

转载自blog.csdn.net/qq_38567039/article/details/89098918