JS监听关闭浏览器事件之Onunload与Onbeforeunload

Onunload与Onbeforeunload 

Onunload,onbeforeunload都是在刷新或关闭时调用,可以在     
  •      
  •   
  • "text/javascript">   
  •    
  •    

  • Java代码 
    这段代码在FF和IE上都能正确执行.再点击关闭按钮时首先触发obbeforeunload事件,点击否定时不执行onload事件.  

    这段代码在FF和IE上都能正确执行.再点击关闭按钮时首先触发obbeforeunload事件,点击否定时不执行onload事件.Java代码 
    通常应用在 注销session等等登陆信息 等方面....  

    通常应用在 注销session等等登陆信息 等方面....Java代码 
    这里一并推荐一个ActionScript3的好教程: http://gskinner.com/talks/as3workshop/  

    这里一并推荐一个ActionScript3的好教程: http://gskinner.com/talks/as3workshop/写道 
    运用onunload事件判断浏览器是刷新还是关闭窗口 
      
    写道 
    Java代码
      收藏代码
    1. function CloseOpen(event) {   
    2. if(event.clientX<=0 && event.clientY<0) {   
    3. alert("关闭");   
    4. }   
    5. else   
    6. {   
    7. alert("刷新或离开");   
    8. }   
    9. }   


      Java代码 
    Java代码
      收藏代码
    1. window.onbeforeunload = function() //author: meizz      
    2.        {      
    3.               var n = window.event.screenX - window.screenLeft;      
    4.               var b = n > document.documentElement.scrollWidth-20;      
    5.               if(b && window.event.clientY < 0 || window.event.altKey)      
    6.               {      
    7.                      alert("是关闭而非刷新");      
    8.                      window.event.returnValue = ""//这里可以放置你想做的操作代码      
    9.               }      
    10.        }    
    11.   
    12.   
    13.   
    14. function window.onbeforeunload()  
    15. {  
    16.   if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey)  
    17.   {  
    18.     window.event.returnvalue = "";  
    19.   }  
    20. }  
    21.   
    22.   
    23. 或者使用全屏打开页面  
    24.   
    25. "javascript">  
    26.   
    27.   
    28.   
    29. 注:在body标签里加上οnbefοreunlοad="javascript:return false"(使不能关闭窗口)或者在frameset标签里加:"100,*" cols="*" frameborder="NO" border="10" framespacing="0" bordercolor="#00FF00"   bordercolor="#00FF00" οnbefοreunlοad="m_close('${ctx}/base/onlines/onlineinfo!prepareRemoveSession.action','106');" id="oneframeset">  
    30.   
    31.   
    32. ==================================================================  
    33.   
    34. function openurl()   
    35. {   
    36. //需要打开的地址   
    37. koyoz.launchURL('http://www.kanshule.com');   
    38. }   
    39. function openinit()   
    40. {   
    41. document.body.innerHTML += ' + 'D3-B153-00C04F79FAA6">';   
    42. }   
    43. eval("window.attachEvent('onload',openinit);");   
    44. eval("window.attachEvent('onunload',openurl);");  
    45.   
    46.   
    47. ===================================================================  
    48. function getEvent() //同时兼容ie和ff的写法    
    49.     {      
    50.         if(document.all)   return window.event;      
    51.         func=getEvent.caller;      
    52.         while(func!=null){      
    53.             var arg0=func.arguments[0];      
    54.             if(arg0)      
    55.             {      
    56.                 if((arg0.constructor==Event || arg0.constructor ==MouseEvent) || (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation))      
    57.                 {      
    58.                     return arg0;      
    59.                 }      
    60.             }      
    61.             func=func.caller;      
    62.         }      
    63.         return null;      
    64.     }    
    65.   
    66.   
    67.   
    68. window.onbeforeunload = function(){   
    69.     var n = window.event.screenX - window.screenLeft;      
    70.     var b = n > document.documentElement.scrollWidth-20;      
    71.     if(b && window.event.clientY < 0 || window.event.altKey)      
    72.     {     
    73.         if(confirm("是否有参与网上调查?")){  
    74.         koyoz.launchURL('http://www.baidu.com');   
    75.         }   
    76.     }      
    77. }   

    ==================================================== 
    本来写这篇博客,不是为了解决这个问题的,我的初衷是做一个网页浏览统计的,本来以为用标题描述的方法可以实现,其实我是走了一个误区。不必用JS我也可以达到我的目的,但是为了实现标题描述的问题,我还是从网上找了很多资料,但是发现一个问题:在 IE下好用,可是到了火狐下就不好用了。于是乎,我做了一些测试,发现以下方法可以在IE和火狐下通用: 
    Java代码
      收藏代码
    1. "text/javascript">  
    2. function close(evt) //author: sunlei  
    3. {  
    4.     var isIE=document.all?true:false;  
    5.     evt = evt ? evt :(window.event ? window.event : null);  
    6.     if(isIE){//IE浏览器  
    7.         var n = evt.screenX - window.screenLeft;  
    8.         var b = n > document.documentElement.scrollWidth-20;  
    9.         if(b && evt.clientY<0 || evt.altKey){  
    10.             alert("是关闭而非刷新");  
    11.         }  
    12.         else{  
    13.             alert("是刷新而非关闭");  
    14.         }  
    15.     }  
    16.     else{//火狐浏览器  
    17.         if(document.documentElement.scrollWidth!=0)  
    18.             alert("是刷新而非关闭");  
    19.         else  
    20.             alert("是关闭而非刷新");  
    21.     }  
    22. }  
    23.   
    24. "close(event);">  
    25.         其中参数event是一定要传进去的,因为在火狐下如果不传的话,它会报错:window.event is not defined。当然,在IE下如果不传的话,是没有问题的。  
    26.         不过细心的人会发现,其实在火狐下进行判断的时候根本没有用到evt。其实把evt传进去,只是为了保证浏览器不会报错,其实可以做如下修改,效果是一样的:  

    =========================================================================== 

    Java代码
      收藏代码
    1. script language=javascript window.onbeforeunload = function() //author: meizz { var n = window.event.screenX - window.screenLeft; var b = n document.documentElement.scrollWidth-20; if(b window.event.clientY 0 || window.event.altKey) { aler  
    2.   
    3. "javascript">    
    4. window.onbeforeunload   =   function()     //author:   meizz    
    5. {    
    6.       var   n   =   window.event.screenX   -   window.screenLeft;    
    7.       var   b   =   n   >   document.documentElement.scrollWidth-20;    
    8.       if(b   &&   window.event.clientY   <   0   ||   window.event.altKey)    
    9.       {    
    10.           alert("是关闭而非刷新");    
    11.           window.event.returnValue   =   "";     //这里可以放置你想做的操作代码  
    12.            
    13.       }else{  
    14.           alert("是刷新而非关闭");    
    15.      }    
    16. }    
    17.   
    18.   
    19.   
    20.   
    21.    
    22.   
    23. function window.onbeforeunload() {  
    24.            if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey){  
    25.              window.event.returnValue="如果离开该页面,将有可能无法获得诚信标签";  
    26.            }else {  
    27.             alert("你在刷新") ;  
    28.            }  
    29.        }  
    30.   
    31.   
    32.   
    33.   
    34.   
    35.   
    36. 判断是刷新还是关闭-www.51windows.Net  
    37. "Content-Type" content="text/html; charset=gb2312">  
    38. "Author" CONTENT="51windows,海娃,haiwa">  
    39. "Description" CONTENT="Power by 51windows.Net">  
    40.   
    41.   
    42.   
    43. "CloseOpen(event)">  
    44.   
    45.   
    46. "position: absolute; top: 10; right: 10; width: 148; height: 18;cursor:hand">  
    47. "button" name="Button" value="查看源代码" onClick= 'window.location = "view-source:" + window.location.href'>
      
  •    



  • --------------------------------------------------------------------------------------------------------------- 

    Java代码
      收藏代码
    1.     
    2.       function window.onbeforeunload()    
    3.       {    
    4.       if    (event.clientX>document.body.clientWidth       &&       event.clientY<0||event.altKey)  
    5.   
    6.             {    
    7.              window.event.returnValue="确定要退出本页吗?";    
    8.             }   
    9.   
    10.       }  
    11.   


    --------------------------------------------------------------------------------------------------------------- 

    Java代码
      收藏代码
    1.   
    2. function window.onbeforeunload()  
    3. {  
    4. if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey)  
    5. {  
    6. var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");  
    7. xmlhttp.open("GET","<%= request.getContextPath()%>" + "/logout.do",false);  
    8. xmlhttp.send();  
    9. }  
    10. }  
    11.   


    --------------------------------------------------------------------------------------------------------------- 

    Java代码
      收藏代码
    1.   
    2. function check()  
    3. {  
    4. if (event.clientX>document.body.clientWidth-20 && event.clientY<0||event.altKey)  
    5. window.event.returnValue='确定要退出本页吗?';  
    6. }  
    7.   
    8.   
    9.   
    10. "check();">  
    11.   


    --------------------------------------------------------------------------------------------------------------- 

    Java代码
      收藏代码
    1.     
    2. function   check()    
    3. {    
    4. if   (event.clientX>document.body.clientWidth-20   &&   event.clientY<0||event.altKey)    
    5.    if(confirm("您确定要离开系统么?"))    
    6.    {    
    7.    window.location.href="logout.jsp";    
    8.     closes.Click();    
    9.     return;    
    10.    }    
    11.    else  
    12.    {  
    13.     window.location.href="main.jsp";    
    14.    }  
    15. }    
    16.   

    ===============================================================================================

    • JS监听关闭浏览器事件.rar (3.5 KB)
    • 下载次数: 19

    你可能感兴趣的:(Web开发)