<body>標(biāo)簽只有onload\onunload\onbeforeunload事件,而沒有onclose事件。不管頁面是關(guān)閉還是刷新都會執(zhí)行onunload事件。如何捕捉到頁面關(guān)閉呢?
頁面加載時(shí)只執(zhí)行onload
頁面關(guān)閉時(shí)只執(zhí)行onunload
頁面刷新時(shí)先執(zhí)行onbeforeunload,然后onunload,最后onload。這樣我們可以在onbeforeunload中加一個(gè)標(biāo)記,在onunload中判斷該標(biāo)記,即可達(dá)到判斷頁面是否真的關(guān)閉了。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建網(wǎng)頁 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('這是關(guān)閉頁面');
}
function fload()
{
?? alert("load me!="+s);
}
function bfunload()
{
?? s = "no";
}
</script>
</body>
</html>