js区分浏览器关闭 or 刷新

js浏览器关闭 or 刷新

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>浏览器关闭 or 刷新</title>
</head>

<body>
<input type="button" onclick="disp_confirm()" value="Display a confirm box" />
<script type="text/javascript">
function disp_confirm() {
localStorage.setItem("text", "111111111111111111111111")
}

var time = {
beginTime: 0,//执行onbeforeunload的开始时间
differTime: 0//时间差
}
window.onunload = function () {
time.differ = new Date().getTime() - time.begin;
if (time.differ <= 3) {
localStorage.clear();
} else {
console.log("浏览器刷新")
}
}
window.onbeforeunload = function () {
time.begin = new Date().getTime();
};
</script>
</body>

</html>

js区分浏览器关闭 or 刷新
https://github.com/chergn/chergn.github.io/ea1d753633ea/
作者
全易
发布于
2024年3月28日
许可协议