mounted() {
this.enterPage()
},
destroyed(){
this.leavePage()
},
methods: {
/* 打开:load
关闭:beforeload+unload
刷新:beforeload+unload+load*/
//离开清除监听
leavePage(){
window.removeEventListener('beforeload', e => this.beforeunloadHandler(e))
window.removeEventListener('unload', e => this.onunload(e))
window.removeEventListener('load', e => this.load(e))
},
//页面进入监听
enterPage(){
let is_fireFox = navigator.userAgent.indexOf("Firefox")>-1;//是否是火狐浏览器
this.is_fireFox = is_fireFox
//添加监听事件
if(is_fireFox){
window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
window.addEventListener('unload', e => this.onunload(e))
}else {
window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
window.addEventListener('unload', e => this.onunload(e))
window.addEventListener('load', e => this.load(e))
}
},
//页面刷新,关闭,打开监听
pagelister(){
if(this.time>-20){
this.$store.commit("setAuth","" );
sessionStorage.removeItem('userCompanys')
sessionStorage.removeItem('userInformation')
if(sessionStorage.getItem("activeCity")){
sessionStorage.removeItem('activeCity')
}
this.maps.removeCookie("auth")
localStorage.setItem('page','关闭')
}else {
localStorage.setItem('page','刷新')
}
},
beforeunloadHandler(e){
this.time = new Date().getTime();
if(this.is_fireFox){
this.time = -5
this.pagelister()
}
},
onunload(){
let onunloadTime = new Date().getTime();
this.time = Number(this.time) - onunloadTime
this.pagelister()
},
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/184146.html原文链接:https://javaforall.cn