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

时间:2022-10-19 23:51:13

页面加载时只执行onload

页面关闭时只执行onunload

页面刷新时先执行onbeforeunload,然后onunload,最后onload

html+js代码:



<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>