(转贴)
<body>标签只有onload/onunload/onbeforeunload事件,而没有onclose事件。不管页面是关闭还是刷新都会执行onunload事件。如何捕捉到页面关闭呢?
页面加载时只执行onload
页面关闭时只执行onunload
页面刷新时先执行onbeforeunload,然后onunload,最后onload。这样我们可以在onbeforeunload中加一个标记,在onunload中判断该标记,即可达到判断页面是否真的关闭了。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页 1</title>
</head>
<body onunload=fclose() onload=fload() onbeforeunload=bfunload()>
<script>
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>
本文介绍了一种通过在HTML页面中使用特定事件监听器来区分页面关闭与刷新的方法。利用onbeforeunload设置标记,在onunload事件中检查该标记,从而判断页面是否真正被关闭。文章提供了一个简单的示例代码,展示了如何实现这一功能。
4319

被折叠的 条评论
为什么被折叠?



