页面关闭和刷新事件

var _t;
    window.onbeforeunload = function(){
        closeTime();
        setTimeout(function(){_t = setTimeout(onunloadcancel, 0)}, 0);
        return "真的离开?";
    }
    
    window.onunloadcancel = function(){
        clearTimeout(_t);
        //alert("取消离开");
    }
原文地址:https://www.cnblogs.com/macliu/p/4019514.html