Js判断页面是关闭还是刷新

  1. 根据页面关闭时的事件判断
    https://blog.csdn.net/u010175124/article/details/9092899
    页面加载时只执行onload
    页面关闭时只执行onunload
    页面刷新时先执行onbeforeunload,然后onunload,最后onload
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
<html> 
<head>
<title>判断页面是关闭还是刷新</title>
</head>

<body onunload="fclose();" onload="fload();" onbeforeunload="bfunload();">
<script language="javascript">
var s = "test";
function fclose()
{
if(s=="no")
alert(’unload me!=’+s+’这是刷新页面!’);
else
alert(’这是关闭页面’);
}

function fload()
{
alert("load me!="+s);
}

function bfunload()
{
s = "no";
}
</script>
</body>
</html>
  1. 根据 performance.navigation.type判断
    https://www.zhihu.com/question/29036668
    0表示从链接进去,1表示刷新

  2. 根据window.name判断
    https://www.zhihu.com/question/29036668

    type
    1
    2
    3
    4
    5
    6
    7
          window.onload = function() {
    if (window.name == "") { // 直接进来才是空的
    alert('直接进来');
    }
    window.name = "test"; // 刷新当前页面,window.name并不会销毁
    };
    </script>
Share