参考:https://www.jb51.net/article/44764.htm
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
vue 监听当前页面关闭
<script>
window.onbeforeunload = function (e) {
e = e || window.event;
// 兼容IE8和Firefox 4之前的版本
if (e) {
e.returnValue = '关闭提示';
}
// Chrome, Safari, Firefox 4+, Opera 12+ , IE 9+
alert('关闭提示')
return '关闭提示';
};
// methods: {
// beforeunloadHandler(e) {
// // ...
// }
// }
// mounted() {
// window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
// }
// destroyed() {
// window.removeEventListener('beforeunload', e => this.beforeunloadHandler(e))
// }
// window 监听卸载事件
// window.onload事件 设置页面加载时执行的动作,即进入页面的时候执行的动作。
// window.onunload 已经从服务器上读到了需要加载的新的页面,在即将替换掉当前页面时调用,一般用于设置当离开页面以后执行的动作。
// window.onbeforeunload 是正要去服务器读取新的页面时调用,此时还没开始读取,简单来说就是 在离开页面前的,一般用做提醒问你是不是要离开这个页面。
// onunload和onbeforeunload都是在页面刷新和关闭前的动作,但是onbeforeunload是先于onunload的,并且 onunload是无法阻止页面的更新和关闭的。而 Onbeforeunload 可以做到
// mounted () {
// window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
// window.addEventListener('unload', e => this.unloadHandler(e))
// },
// destroyed () {
// window.removeEventListener('beforeunload', e => this.beforeunloadHandler(e))
// window.removeEventListener('unload', e => this.unloadHandler(e))
// },
// methods: {
// beforeunloadHandler (e) {
// // debugger
// this._beforeUnload_time = new Date().getTime()
// console.log('this._beforeUnload_time:', this._beforeUnload_time)
// e = e || window.event
// if (e) {
// e.returnValue = '关闭提示'
// }
// // debugger
// return '关闭提示'
// },
// unloadHandler () {
// console.log('this._beforeUnload_time2:', this._beforeUnload_time)
// this._gap_time = new Date().getTime() - this._beforeUnload_time
// console.log('this._gap_time:', this._gap_time)
// // 判断是窗口关闭还是刷新
// if (this._gap_time <= 5) {
// // debugger
// // 如果是登录状态,关闭窗口前,移除用户
// if (!this.showLoginButton) {
// $.ajax({
// url: '/pictureweb/user/remove',
// type: 'get',
// async: false // 或false,是否异步
// })
// }
// } else {
// // debugger
// }
// }
// }
// 你可以这么写: location.replace(location.href);
// 返回并刷新页面:
// location.replace(document.referrer);
// document.referrer //前一个页面的URL
// 不要用 history.go(-1),或 history.back(); 来返回并刷新页面,这两种方法不会刷新页面。
// Javascript刷新页面的几种方法:
// 1 history.go(0)
// 2 location.reload()
// 3 location = location
// 4 location.assign(location)
// 5 document.execCommand('Refresh')
// 6 window.navigate(location)
// 7 location.replace(location)
// 8 document.URL = location.href
// 自动刷新页面的方法:
// 1.页面自动刷新:把如下代码加入 < head > 区域中
// < meta http - equiv="refresh" content = "20" >
// 其中20指每隔20秒刷新一次页面.
// 2.页面自动跳转:把如下代码加入 < head > 区域中
// < meta http - equiv="refresh" content = "20;url=https://www.jb51.net" >
// 其中20指隔20秒后跳转到https://www.jb51.net页面
</script>
3.页面自动刷新js版
<script language="JavaScript">
// function myrefresh() {
// window.location.reload();
// }
// setTimeout('myrefresh()', 1000); //指定1秒刷新一次
</script>
JS刷新框架的脚本语句
//如何刷新包含该框架的页面用
<script language=JavaScript>
// parent.location.reload();
</script>
//子窗口刷新父窗口
<script language=JavaScript>
self.opener.location.reload();
</script>
( 或 <a href="javascript:opener.location.reload()">刷新</a> )
是大的区别还是是否提交数据了
<script>
function refresh() {
//刷新页面函数
//window.focus();刷新窗口
//document.execCommand("Refresh");刷新窗口
// //self.location.reload();刷新当前窗口
// parent.location.reload(); 刷新父窗口
//aaa.location.reload();弹出窗口刷新父窗口
}
// 使用window.location.replace() window.location.href(), 来重新加载此页面不出现提示框
</script>
</body>
</html>