在许多网站上看到这样的一个效果:
就是在用户离开或者关闭当前页面时,会弹出一个确认对话框,
当用户确认离开时,点击确定则关闭当前页面,
如果说用户是误操作,不愿离开当前页面,点击取消
则继续留在当前页面。
请问这样的效果如何实现?
说一说原理也行!谢谢了!!!

解决方案 »

  1.   

    在body的onbeforeunload事件里confirm(“确定离开?”)
      

  2.   

    不用confirm,直接
    window.onbeforeunload = function (){ 
    return "xxxx"; 
    }
      

  3.   

    <script language="javascript" type="text/javascript">
    var g_blnCheckUnload = true;
    function RunOnBeforeUnload() 
    {
        if(g_blnCheckUnload)
        {
            window.event.returnValue = '确认关闭窗口吗?'; 
        } 

    function bypassCheck() 

        g_blnCheckUnload  = false;
    }
    onbeforeunload=RunOnBeforeUnload;
    </script>
    </head>
    <body onbeforeunload="RunOnBeforeUnload()">
      

  4.   

    <script language="javascript" type="text/javascript">
    var g_blnCheckUnload = true;
    function RunOnBeforeUnload() 
    {
        if(g_blnCheckUnload)
        {
            window.event.returnValue = '确认关闭窗口吗?'; 
        } 

    function bypassCheck() 

        g_blnCheckUnload  = false;
    }
    onbeforeunload=RunOnBeforeUnload;
    </script>
    </head>
    <body onbeforeunload="RunOnBeforeUnload()">
      

  5.   

    返回值是聋子的耳朵,没用的需要注意的是,FF下要把event作为参数传递进函数
    <script>
    function aaa(e){
      e=e||window.event
      e.returnValue="真的要离开本页面吗?"
    }
    </script>
    <body onbeforeunload="aaa(event)">
      

  6.   

    <html>
    <head>
    <script> 
    function  window.onbeforeunload()  {  
     if  (event.clientX>document.body.clientWidth  &&  event.clientY<0 ||event.altKey)  
     window.event.returnValue="";  
     }
    </script>
    <head>
    <body>
    <a href="#" onClick="window.close();">注销</a> 
    </body>
    </html>