ä¸ä¸ªå¤æ页é¢æ¯å¦ççå
³éåå·æ°ç好æ¹æ³ï¼
window.onbeforeunload=function (){
alert("===onbeforeunload===");
if(event.clientX>document.body.clientWidth && event.clientY < 0 || event.altKey){
alert("ä½ å
³éäºæµè§å¨");
}else{
alert("ä½ æ£å¨å·æ°é¡µé¢");
}
}
è¿æ®µä»£ç å°±æ¯å¤æ触åonbeforeunloadäºä»¶æ¶ï¼é¼ æ æ¯å¦ç¹å»äºå
³éæé®ï¼æè
æäºALT+F4æ¥å
³éç½é¡µï¼å¦ææ¯ï¼å认为系ç»æ¯å
³éç½é¡µï¼å¦åå¨è®¤ä¸ºç³»ç»æ¯å·æ°ç½é¡µã
onbeforeunloadä¸onunloadäºä»¶
Onunloadï¼onbeforeunload
é½æ¯å¨å·æ°æå
³éæ¶è°ç¨ï¼å¯ä»¥å¨<script>èæ¬ä¸éè¿window.onunload
æ¥æå®æè
å¨<body>éæå®ãåºå«å¨äºonbeforeunloadå¨onunloadä¹åæ§è¡ï¼å®è¿å¯ä»¥é»æ¢onunloadçæ§è¡ã
Onbeforeunload
ä¹æ¯å¨é¡µé¢å·æ°æå
³éæ¶è°ç¨ï¼Onbeforeunloadæ¯æ£è¦å»æå¡å¨è¯»åæ°ç页é¢æ¶è°ç¨ï¼æ¤æ¶è¿æ²¡å¼å§è¯»åï¼èonunloadåå·²ç»ä»æå¡å¨ä¸è¯»
å°äºéè¦å è½½çæ°ç页é¢ï¼å¨å³å°æ¿æ¢æå½å页é¢æ¶è°ç¨ãOnunloadæ¯æ æ³é»æ¢é¡µé¢çæ´æ°åå
³éçãèOnbeforeunload å¯ä»¥åå°ã
页é¢å è½½æ¶åªæ§è¡onload
页é¢å
³éæ¶å
æ§è¡onbeforeunloadï¼æåonunload
页é¢å·æ°æ¶å
æ§è¡onbeforeunloadï¼ç¶åonunloadï¼æåonloadã
1ãonbeforeunloadäºä»¶ï¼
说æï¼ç®åä¸å¤§ä¸»æµæµè§å¨ä¸firefoxåIEé½æ¯æonbeforeunloadäºä»¶,operaå°æªæ¯æã
ç¨æ³ï¼
·object.onbeforeunload = handler
·<element onbeforeunload = âhandlerâ ⦠></element>
æè¿°ï¼
äºä»¶è§¦åçæ¶åå¼¹åºä¸ä¸ªæç¡®å®ååæ¶ç对è¯æ¡ï¼ç¡®å®å离å¼é¡µé¢ï¼åæ¶å继ç»å¾
å¨æ¬é¡µãhandlerå¯ä»¥è®¾ä¸ä¸ªè¿åå¼ä½ä¸ºè¯¥å¯¹è¯æ¡çæ¾ç¤ºææ¬ã
触åäºï¼
·å
³éæµè§å¨çªå£
·éè¿å°åæ ææ¶è夹åå¾å
¶ä»é¡µé¢çæ¶å
·ç¹å»è¿åï¼åè¿ï¼å·æ°ï¼ä¸»é¡µå
¶ä¸ä¸ä¸ªçæ¶å
·ç¹å» ä¸ä¸ªåå¾å
¶ä»é¡µé¢çurlè¿æ¥çæ¶å
·
è°ç¨ä»¥ä¸ä»»æä¸ä¸ªäºä»¶çæ¶åï¼clickï¼document writeï¼document openï¼document closeï¼window
close ï¼window navigate ï¼window NavigateAndFind,location replace,location
reload,form submit.
·å½ç¨window openæå¼ä¸ä¸ªé¡µé¢ï¼å¹¶ææ¬é¡µçwindowçååä¼ ç»è¦æå¼ç页é¢çæ¶åã
·éæ°èµäºlocation.hrefçå¼çæ¶åã
·éè¿input type=âsubmitâæé®æ交ä¸ä¸ªå
·ææå®actionç表åçæ¶åã
å¯ä»¥ç¨å¨ä»¥ä¸å
ç´ ï¼
·BODY, FRAMESET, window
å¹³å°æ¯æï¼
IE4+/Win, Mozilla 1.7a+, Netscape 7.2+, Firefox0.9+
示ä¾ï¼
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>onbeforeunloadæµè¯</title>
<script>
function checkLeave(){
event.returnValue="ç¡®å®ç¦»å¼å½å页é¢åï¼";
}
</script>
</head>
<body onbeforeunload="checkLeave()">
</body>
</html>
2ãonunloadäºä»¶
ç¨æ³ï¼
·object.onbeforeunload = handler
·<element onbeforeunload = "handler"></element>
æè¿°ï¼
å½ç¨æ·å
³éä¸ä¸ªé¡µé¢æ¶è§¦åonunload äºä»¶ã
触åäºï¼
·å
³éæµè§å¨çªå£
·éè¿å°åæ ææ¶è夹åå¾å
¶ä»é¡µé¢çæ¶å
·ç¹å»è¿åï¼åè¿ï¼å·æ°ï¼ä¸»é¡µå
¶ä¸ä¸ä¸ªçæ¶å
·ç¹å» ä¸ä¸ªåå¾å
¶ä»é¡µé¢çurlè¿æ¥çæ¶å
·
è°ç¨ä»¥ä¸ä»»æä¸ä¸ªäºä»¶çæ¶åï¼clickï¼document writeï¼document openï¼document closeï¼window
close ï¼window navigate ï¼window NavigateAndFind,location replace,location
reload,form submit.
·å½ç¨window openæå¼ä¸ä¸ªé¡µé¢ï¼å¹¶ææ¬é¡µçwindowçååä¼ ç»è¦æå¼ç页é¢çæ¶åã
·éæ°èµäºlocation.hrefçå¼çæ¶åã
·éè¿input type=âsubmitâæé®æ交ä¸ä¸ªå
·ææå®actionç表åçæ¶åã
示ä¾ï¼
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>onunloadæµè¯</title>
<script>
function checkLeave(){
alert("欢è¿ä¸æ¬¡åæ¥ï¼");
}
</script>
</head>
<body onunload="checkLeave()">
</body>
</html>