代码改变世界

window.onbeforeunload 事件的研究

2013-03-29 17:45  海边拾贝壳的人  阅读(276)  评论(0)    收藏  举报

window.onbeforeunload = function() {
     // in Firefox and Netscape 7.2+ the setTimeout or setInterval do not wait
     // to be executed until after the user clicks one of the buttons in the
     // confirm()-like box.
     //setTimeout("alert('hi from setTimeout()');",500);
     // setTimeout() and setInterval() aren't called when ok is clicked in
     // IE5-6/Win, but is called in IE7 when the time is short, but not when
     // it's longer, like 500 (a half second).
     window.unloadTimer = setInterval("alert('hi from          setInterval()');clearInterval(window.unloadTimer);",500);
    
     window.onunload = function() {
         clearInterval(window.unloadTimer);
     }
     return 'onbeforeunload testing';
}

上面这段代码  在ie8/firefox choreme 上都能正常运行  在 oprea上 关闭窗口时不会触发这个事件

模型1:
function close(){ 
alert("sdfsdfsdf");

window.onbeforeunload=close;

模型2:
function close(){ 
if(document.body.clientWidth-event.clientX< 170&&event.clientY< 0||event.altKey) 

alert("sdfsdfsdf");
}  

window.onunload=close;

对于模型1:
1).刷新,多窗口和单窗口都适合.
2).单窗口ie关闭整个ie触发.
3).ie7多窗口中关闭单页触发
4)其他多窗口刷新触发.关闭单个和关闭整个都不触发

对于模型2:
1).ie单窗口 和ie7多窗口,都要关闭整个浏览器才触发
2).其他多窗口浏览器刷新.关闭单页,关闭整个都不触发