我希望页面关闭时触发一段javascript代码,但是关闭标签页和关闭浏览器不触发onunload()事件,不知道该怎么办。或者请高手告诉我如何判断标签页或者浏览器关闭。

解决方案 »

  1.   

    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("是关闭而非刷新");
                } else {
                    alert("是刷新而非关闭");
                }
            }
      

  2.   


    //考虑到兼容
    <script>

    function window.onbeforeunload() 
       { 
              if(event.clientX>360&&event.clientY<0)
              { 
                    window.event.returnValue="提示:\“退出系统\”请点系统的\“安全退出\”!"; 
              } 
       }
    $(document).bind("onbeforeunload",function(){return false;})

    </script>
    </head><body onbeforeunload='return "提示:\“退出系统\”请点系统的\“安全退出\”!"'></body>
    </html>
      

  3.   

    //考虑到兼容
    <script>
        
        function window.onbeforeunload() 
       { 
              if(event.clientX>360&&event.clientY<0)
              { 
                    window.event.returnValue="提示:\“退出系统\”请点系统的\“安全退出\”!"; 
              } 
       }
        //$(document).bind("onbeforeunload",function(){return false;})
        
    </script>
    </head><body onbeforeunload='return "提示:\“退出系统\”请点系统的\“安全退出\”!"'></body>
    </html>
      

  4.   

    onbeforeunload()我试过也不行啊。我的是IE9浏览器。