关闭浏览器执行事件,刷新不执行

var _beforeUnload_time =0, _gap_time =0;

var is_fireFox = navigator.userAgent.indexOf("Firefox") > -1;//是否是火狐浏览器

window.onunload =function () {

_gap_time =new Date().getTime() - _beforeUnload_time;

    if (_gap_time <=5) {

console.log('关闭');


}

else {

console.log('刷新');

    }

}

window.onbeforeunload =function () {

_beforeUnload_time =new Date().getTime();

    //火狐关闭执行

    if (is_fireFox) {

cookie.save('token', '', {

path:'/',

            maxAge:14 *24 *60 *60,

        })

}

// $.post(pathName + "/back/bi!aaaa.s2?t=" + _beforeUnload_time, { msg: "火狐关闭" }, function (json) { }, "text");

}

修改,上面的有时会不好用,下面的改变一下思路

window.onload =function () {

if (!window.sessionStorage["tempFlag"]) {//关闭

        window

.localStorage

            .removeItem("userVal")

cookie.save('token', '', {

path:'/',

            maxAge:14 *24 *60 *60,

        })

location.reload();

    }else {//刷新

        window.sessionStorage.removeItem("tempFlag");

    }

}

window.onunload =function () {

window.sessionStorage["tempFlag"] =true;

}

window.onbeforeunload =function () {

window.sessionStorage["tempFlag"] =true;

}

你可能感兴趣的:(关闭浏览器执行事件,刷新不执行)