我在javascript里面加入了这样一段代码
function window.onunload()   
{   
    if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey||event.ctrlKey)   
{    
     alert('关闭');   
}   
else{   
     alert("刷新");   
}   } 但是不管我是刷新还是关闭网页,都只弹出 “刷新” ,不能弹出关闭
 各位大哥帮忙解决一下这个问题,就是把刷新和关闭区分出来

解决方案 »

  1.   

    function window.onunload()  刷新的时候不会调用吧
      

  2.   

    IE明确指出,以下会动作会触发onunload事件Close the current browser window. 
    Navigate to another location by entering a new address or selecting a Favorite. 
    Click the Back, Forward, Refresh, or Home button. 所以刷新和关闭应该是不能区分的
      

  3.   


    function CloseOpen(event) {
        if(event.clientX<=0 && event.clientY<0) {
            alert("关闭");
        }else{
            alert("刷新或离开");
        }


    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){   
           alert("是关闭而非刷新");   
           window.event.returnValue = ""; //这里可以放置你想做的操作代码   
       }else{
           alert("是刷新而非关闭");   
       }  
    }  
    参考于:http://www.javaeye.com/topic/269213
      

  4.   

    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){   
           alert("是关闭而非刷新");   
           window.event.returnValue = ""; //这里可以放置你想做的操作代码   
       }else{
           alert("是刷新而非关闭");   
       }  
    }  
      

  5.   

    meizz大侠的那段代码..在通过双击标题栏前面的图标/浏览器的右键控制菜单关闭时无法判断.
      

  6.   

    上面这2段代码在ie6里面的确可以监听到,但是在ie7就不行了
    上面代码要求的是一个浏览器只有一个网页,而ie7是一个浏览器有多个网页标签,这样的话上面的代码就行不通了
      

  7.   

    这对ie7以上的浏览器和ff,多标签浏览时,上述代码就无法判断是否为关闭标签的操作了