您的位置:首页 > 其它

如何判断一个网页是刷新还是关闭的方法。

2006-06-26 19:57 627 查看
(转贴)

<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: