var currentKeyCode = -1;    function document.onkeydown() { // 本窗口的所有下属页面都必须含有本函数       top.currentKeyCode = event.keyCode;
    }
    function window.onbeforeunload() {        var sw = 0, s0 = "";
        if (currentKeyCode == 116) 
        {
           s0 += "刷新窗口!(F5)";
        }
        else 
        {
           if ((event.altKey)&&(currentKeyCode == 115)) 
            {
                  s0 += "关闭窗口!(alt+F4)"; sw = 1;
            }
            else 
            {
               if ((event.clientX > 0)&&(event.clientX < document.body.clientWidth)) 
               {
                  s0 += "刷新窗口!";
               }
               else 
               {
                  s0 += "关闭窗口!"; sw = 1;
               }
            }
        }
        if (sw == 1) 
        {
          event.returnValue = "";
        }
        else 
        {
          currentKeyCode = -1;
        }
    }
这是我从网上找的一段JS代码,用浏览器右上角的关闭按钮时好用,但在选项卡上关闭和在任务栏上关闭,这个方法就不作用了,我用的是IE8

解决方案 »

  1.   

    window对象是不是有个onclosing事件啊?
      

  2.   

    我记得windows是有一个关闭事件的。
      

  3.   


    web里面也同样可以用这个方法吗?
      

  4.   

    js标签只有onload\onunload\onbeforeunload事件,而没有onclose事件。不管页面是关闭还是刷新都会执行onunload事件。如何捕捉到页面关闭呢? 
    页面加载时只执行onload 
    页面关闭时只执行onunload 
    页面刷新时先执行onbeforeunload,然后onunload,最后onload。这样我们可以在onbeforeunload中加一个标记,在onunload中判断该标记,即可达到判断页面是否真的关闭了。1、Js代码 
    1.window.onbeforeunload = function() {    
    2.  
    3.var n = window.event.screenX - window.screenLeft;    
    4.  
    5.var b = n > document.documentElement.scrollWidth-20;    
    6.  
    7.  
    8.if(b && window.event.clientY < 0 || window.event.altKey) {    
    9.  
    10.alert("是关闭而非刷新");    
    11.  
    12.window.open(this.location);      
    13.  
    14.  
    15.//return false;   
    16.  
    17.//window.event.returnValue = ""; }    
    18.  
    19.} else{   
    20.  
    21.alert("是刷新而非关闭");    
    22.  
    23.}   
    24.  
    25.}  
    window.onbeforeunload = function() { var n = window.event.screenX - window.screenLeft; var b = n > document.documentElement.scrollWidth-20; 
    if(b && window.event.clientY < 0 || window.event.altKey) { alert("是关闭而非刷新"); window.open(this.location);   
    //return false;//window.event.returnValue = ""; } } else{alert("是刷新而非关闭"); }}
     2、Js代码 
    1.function promptOnClose(e){   
    2.  
    3.//e = e ? e : windowevent;   
    4.  
    5.var promptString = '你是否要离开此页面,离开该页面的信息将不被保存!';   
    6.  
    7.//event.returnValue = promptString;   
    8.  
    9.return promptString;   
    10.  
    11.}   
    12.  
    13.if (window != top){   
    14.  
    15.top.location.href = "www.baidu.com";   
    16.  
    17.}else{   
    18.  
    19.if (window.Event) {   
    20.  
    21.      window.onbeforeunload = function(event) {   
    22.  
    23.     return promptOnClose(event);   
    24.  
    25.      }   
    26.  
    27.} else {   
    28.  
    29.      window.onbeforeunload = function() {   
    30.  
    31.       return promptOnClose(event);   
    32.  
    33.        }   
    34.  
    35.     }   
    36.  
    37.}  
      

  5.   

    你还是换个思路吧如果浏览器没有标签页 eg: IE6 那可以根据鼠标所在位置判断但有标签页的就行不通了
      

  6.   

    关标签是UNLOAD,CLOSE是关进程才触发的
      

  7.   

    把&& 改成 || 
      

  8.   


    function CloseOpen(event) { 
             if(event.clientX<=0 || event.clientY<0) { 
                 //获取当前时间
               var date=new Date();
               //将date设置为过去的时间
               alert("关闭网页");
               date.setTime(date.getTime()-10000);
               //将userId这个cookie删除
               document.cookie="zhuangtao;expire="+date.toUTCString();
               document.cookie="quanxianzifucuan;expire="+date.toUTCString();
               document.cookie="quanxian;expire="+date.toUTCString();
              s0 += "关闭窗口!"; sw = 1;
                onbeforeunload();
               // window.event.returnValue = '关闭浏览器将退出系统.';
             } 
             else 
             { 
                    alert("刷新或离开"); 
             } 
      } 
                  
                    var currentKeyCode = -1;    function document.onkeydown() { // 本窗口的所有下属页面都必须含有本函数       top.currentKeyCode = event.keyCode;
        }    function onbeforeunload(){
        
         var sw = 0, s0 = "";
            if (currentKeyCode == 116) 
            {
               s0 += "刷新窗口!(F5)";
            }
            else 
            {
               if ((event.altKey)&&(currentKeyCode == 115)) 
                {
                      s0 += "关闭窗口!(alt+F4)"; sw = 1;
                       //获取当前时间
                       var date=new Date();
                       //将date设置为过去的时间
                      alert("关闭窗口");
                       date.setTime(date.getTime()-10000);
                       //将userId这个cookie删除
                       document.cookie="zhuangtao;expire="+date.toUTCString();
                       document.cookie="quanxianzifucuan;expire="+date.toUTCString();
                       document.cookie="quanxian;expire="+date.toUTCString();
                }
                else 
                {
                   if ((event.clientX > 0)&&(event.clientX < document.body.clientWidth)) 
                   {
                      s0 += "刷新窗口!";
                   }
                   else 
                   {
                    //获取当前时间
                       var date=new Date();
                       //将date设置为过去的时间
                       alert("关闭网页");
                       date.setTime(date.getTime()-10000);
                       //将userId这个cookie删除
                       document.cookie="zhuangtao;expire="+date.toUTCString();
                       document.cookie="quanxianzifucuan;expire="+date.toUTCString();
                       document.cookie="quanxian;expire="+date.toUTCString();
                      s0 += "关闭窗口!"; sw = 1;
                   }
                }
            }
            if (sw == 1) 
            {
              event.returnValue = "";
            }
            else 
            {
              currentKeyCode = -1;
            }
        
        }<body  onunload="CloseOpen(event)" ></body></html>
    我最后用的是这个,但还是没法判断在任务栏上直接关闭浏览器,结帖
      

  9.   

    function CloseOpen(event) { 
             if(event.clientX<=0 || event.clientY<0) { 
                 //获取当前时间
               var date=new Date();
               //将date设置为过去的时间
               alert("关闭网页");
               date.setTime(date.getTime()-10000);
               //将userId这个cookie删除
               document.cookie="zhuangtao;expire="+date.toUTCString();
               document.cookie="quanxianzifucuan;expire="+date.toUTCString();
               document.cookie="quanxian;expire="+date.toUTCString();
              s0 += "关闭窗口!"; sw = 1;
                onbeforeunload();
               // window.event.returnValue = '关闭浏览器将退出系统.';
             } 
             else 
             { 
                    alert("刷新或离开"); 
             } 
      } 
                  
                    var currentKeyCode = -1;    function document.onkeydown() { // 本窗口的所有下属页面都必须含有本函数       top.currentKeyCode = event.keyCode;
        }    function onbeforeunload(){
        
         var sw = 0, s0 = "";
            if (currentKeyCode == 116) 
            {
               s0 += "刷新窗口!(F5)";
            }
            else 
            {
               if ((event.altKey)&&(currentKeyCode == 115)) 
                {
                      s0 += "关闭窗口!(alt+F4)"; sw = 1;
                       //获取当前时间
                       var date=new Date();
                       //将date设置为过去的时间
                      alert("关闭窗口");
                       date.setTime(date.getTime()-10000);
                       //将userId这个cookie删除
                       document.cookie="zhuangtao;expire="+date.toUTCString();
                       document.cookie="quanxianzifucuan;expire="+date.toUTCString();
                       document.cookie="quanxian;expire="+date.toUTCString();
                }
                else 
                {
                   if ((event.clientX > 0)&&(event.clientX < document.body.clientWidth)) 
                   {
                      s0 += "刷新窗口!";
                   }
                   else 
                   {
                    //获取当前时间
                       var date=new Date();
                       //将date设置为过去的时间
                       alert("关闭网页");
                       date.setTime(date.getTime()-10000);
                       //将userId这个cookie删除
                       document.cookie="zhuangtao;expire="+date.toUTCString();
                       document.cookie="quanxianzifucuan;expire="+date.toUTCString();
                       document.cookie="quanxian;expire="+date.toUTCString();
                      s0 += "关闭窗口!"; sw = 1;
                   }
                }
            }
            if (sw == 1) 
            {
              event.returnValue = "";
            }
            else 
            {
              currentKeyCode = -1;
            }
        
        }
      

  10.   

    我网上找的,原文是http://www.tedpavlic.com/post_detect_refresh_with_javascript.php原文给了两个方法,方法一大致翻译如下,对于大多数浏览器,表单隐藏域的值在浏览器刷新后仍被保留。所以可以给页面添加一个拥有默认值(本例中为空)的表单隐藏域,如果在pageload事件中此默认值未改变,则说明pageload事件不是被刷新触发的,此时可以给这个表单隐藏域设置一个非默认值;若在pageload事件中此表单隐藏域的值被改变了(在非刷新页面导致的pageload事件中改变的),那么说明此时的pageload事件是被刷新触发的。
    代码如下,
    <html>
    <head><script language="JavaScript"> <!--
    function checkRefresh()
    {
    if( document.refreshForm.visited.value == "" )
    {
    // 非刷新,改变默认值
    document.refreshForm.visited.value = "1"; // 非刷新情况下的其它应用逻辑
    }
    else
    {
    // 刷新页面导致下面代码的执行 // 刷新情况下的其它应用逻辑
    }
    } -->
    </script></head><body onLoad="JavaScript:checkRefresh();"><form name="refreshForm">
    <input type="hidden" name="visited" value="" />
    </form></body>
    </html>我没对代码进行测试,只是路过。。
      

  11.   

    11楼的,关闭 刷新都触发onbeforeunload事件