本帖最后由 zhangzhenting 于 2010-01-22 17:18:16 编辑

解决方案 »

  1.   

    <script language="javascript" type="text/javascript">
    var g_blnCheckUnload = true;
    function RunOnBeforeUnload() 
    {
        if(g_blnCheckUnload)
        {
            window.event.returnValue = 'You will lose any unsaved content'; 
        } 

    function bypassCheck() 

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

  2.   

    window.onbeforeunload = function()
    {
    if (!window.confirm("Are you sure to delete the item? ")){
          return;
     }
    }
      

  3.   

    window.onbeforeunload = function()
    {
        return confirm('确认关闭?');
    }
      

  4.   


    2楼 头像也不赖, 和你本人应该差不多吧 ,呵呵~
    just a joke! Do not be angry
      

  5.   

    这样写? 那个return 什么效果也没有的哦, 就是说 onunload 不管怎样都会执行的;
      

  6.   

    window.onbeforeunload = function() 

    if (!window.confirm("确定关闭页面?")){ 
          //do something 

    else
    {
    //do something}

      

  7.   

    做不到。onbeforeunload这个事件,不能自定义弹窗,只能是return一个字符串给系统,系统有个标准弹窗,但是一旦进入系统这个弹窗,如果客户选关闭,系统就直接处理了,也不会给你机会。所以,你只能做的是,不管客户选择什么,只要是这个事件发生,就通通做一遍。
      

  8.   


    是的,IE6上是不会关闭的;
    但程序还是不太懂 ,bypassCheck 方法貌似都没有被调用的;
    还有 onbeforeunload=RunOnBeforeUnload; 和 <body onbeforeunload="RunOnBeforeUnload()"> 
    都没有重复的必要哦。
    问题是:我提出的2个问题还是没解决,呵呵~
    继续期待中
      

  9.   

    改设计吧 用div实现弹窗效果
      

  10.   

    自己写一个模拟弹出窗口吧。用div来模拟画一个弹出窗口。。在任务栏关闭想要提示貌似这样不行的。
      

  11.   


    嗯, 20-25楼 基本上都想到一块去了。 看来是个不错的主意。
    想问下:如何监听窗口的关闭事件呢? 要自己模拟弹出窗口,那么用 onbeforeunload 这个事件就行不通了。
      

  12.   

    如果要用div模拟弹出窗口的话 关闭按钮 就是自己做的一个普通的按钮, 不需要用onbeforeunload时间
      

  13.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head><body>
    <object classid="clsid:B338F4C7-3375-4FA1-BF22-76503A38EB83" id="TT" onclick="alert(1);">
    </object>
    </body>
    </html>
    <SCRIPT  language=JavaScript>  
    <!--  
    var tt = document.getElementById('TT');window.onbeforeunload = function()
    {
        return false;
    }window.onunload = function()
    {
    tt.onclick();
    }
    //-->  
    </SCRIPT>  
      

  14.   

    <script type="text/javascript">        function UnLoadWindow() {
                return 'We strongly recommends NOT closing this window yet.'
            }        window.onbeforeunload = UnLoadWindow;
       </script>orIf you also want to detect whether the user click the X on the top-right of the browser:function handleWindowClose()
    {
      if((window.event.clientX<0) || (window.event.clientY<0))
        {
          event.returnValue = "If you have made any changes to the fields without clicking the Save button, your changes will be lost.";
        }
    }But I am also afraid you never catch the browser close event for the full 100%. For example, what if the user kills the browser process. Then the onUnload, or even the onBeforeUnload isn't called.
      

  15.   

      window.onbeforeunload = function (){return ""};  so easy
      

  16.   

    这样子只有在关闭时才提示:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <TITLE> New Document </TITLE>
      <META NAME="Generator" CONTENT="EditPlus">
      <META NAME="Author" CONTENT="">
      <META NAME="Keywords" CONTENT="">
      <META NAME="Description" CONTENT="">
      <script type="text/javascript"> function handleWindowClose()
    {
      if((window.event.clientX <0) || (window.event.clientY <0))
        {
          event.returnValue = "If you have made any changes to the fields without clicking the Save button, your changes will be lost.";
        }

            window.onbeforeunload = handleWindowClose;
      </script> 
     </HEAD> <BODY>
      
     </BODY>
    </HTML>
      

  17.   

    function frmClose() 

      if((window.event.clientX <0) || (window.event.clientY <0)) 
        { 
          event.returnValue = "";
        } 
    } window.onbeforeunload =frmClose;
      

  18.   


    呵呵~  看了你在28楼的代码。可能是我没说太清楚吧, 我的意思是 在调用 onunload 事件的时候,窗口已经是关闭的,此时页面上的一些组件(如 .cab)都已经被卸载。 那么一些操作将不会被执行; 而不是你所写的简单的 onclick 方法。
    顺便指出下, 你的那个 onbeforeunload 返回 false 是没有意义的, 只能返回字符串
      

  19.   


    再次说明我的用意:
    我希望能 在调用 onbeforeunload 之后, 选择 确认关闭之后, 而又在调用 onunload 之前, 做些事情。
    原因如下:
    1. 不能在 onbeforeunload 中处理, 因为无法知道 用户 是不是确认关闭。
    2. 不能在 onunload 中处理, 因为在执行该方法时,窗口已经关闭,窗口所加载的一些组件都已经不存在。
    所以 , 还望各位高人 指点指点
      

  20.   

    <input type = "button" id = "btnClose" value = "close" onclick = "window.opener=null;window.open('','_self');window.close();">
      

  21.   

    onunload是有bug的,经常捕捉不到
      

  22.   

    现在的网站和程序都很变态,把你的IE装上插件什么的,阻止了弹出框什么的,onunload都不起作用。
      

  23.   

    用DIV模拟 弹出窗口
    什么问题都解决了
      

  24.   


    window.onbeforeunload = function() //author: meizz    
           {    
                          alert("退出系统!"); 
                         window.location.href="loginOut.aspx"; 
                     TT方法(1000); 
           }   
     loginouu.cs ClientScript.RegisterStartupScript(ClientScript.GetType(), "aa", "<script>window.opener=null;window.close();</script>");
      

  25.   

    confirm  用这个方法即可
      

  26.   


    不太懂。。 在 onbeforeunload 事件中 alert(); 不会起到任何作用的啊!试过了,弹出对话框之后会关闭窗口, 并不会跳转的
      

  27.   

    LZ的问题好像不能解决吧,只要你调用了onunload事件,窗口无论如何都是会关闭的,除非你自己模拟关闭窗口或者自定义关闭按钮,或者你能找到一个调用完onunload事件但却让该事件失效的办法
      

  28.   

    我的源码;
    defalut.aspx  <script>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.location.href="loginOut.aspx"; 
                         sleep(1000); 
                                                                                    
                  }    
           }    function sleep(milisecond) { var currentDate,beginDate=new Date(); var beginHour,beginMinute,beginSecond,beginMs; var hourGaps,minuteGaps,secondGaps,msGaps,gaps; beginHour=beginDate.getHours(); beginMinute=beginDate.getMinutes(); beginSecond=beginDate.getSeconds(); beginMs=beginDate.getMilliseconds(); do { currentDate=new Date(); hourGaps=currentDate.getHours() - beginHour; minuteGaps=currentDate.getMinutes() - beginMinute; secondGaps=currentDate.getSeconds() - beginSecond; msGaps=currentDate.getMilliseconds() - beginMs; if(hourGaps<0) hourGaps+=24; gaps=hourGaps*3600+ minuteGaps*60+ secondGaps; gaps=gaps*1000+msGaps; }while(gaps<milisecond); }     </script>loginout.cs
     
    if (!IsPostBack)
            {
                int linID = Convert.ToInt32(Session["linId"].ToString());
                DataSet dsIP = bll_online.getAllOnline("select loginIP,logintime from tab_online where linID=" + linID);
                string L2 = dsIP.Tables[0].Rows[0]["loginIP"].ToString();
                string L1 = dsIP.Tables[0].Rows[0]["logintime"].ToString();            
                DateTime dt = DateTime.Now;
                Model_online.Leavetime = Convert.ToString(dt);
                // Model_online.EmpID = empid;
                Model_online.Logintime = L1;
                Model_online.LoginIP = L2;
                Model_online.Online = false;
                Model_online.LinID = linID;
                bll_online.updateOnline(Model_online);
                Session.Remove("loginEmployerId");
                //关闭IE
               ClientScript.RegisterStartupScript(ClientScript.GetType(), "aa", "<script>window.opener=null;window.close();</script>");
               
            }
    关闭页面时清空session。并记录退出时间
      

  29.   


    在关闭之前 要给出提示“是否确认关闭?”, 但好像你在 onbeforeunload 中就直接处理了;
    如果用户 取消关闭,那样就不行了吧~
      

  30.   

    window.onbeforeunload = function()
    {
       if(confirm('确定要关闭该页面吗?'))
          {
              window.location.href="loginOut.aspx"; 
                         sleep(1000);       }
    }
    这样可以吗?
      

  31.   


    呵呵~, 要是取消, 那session 都没了。 还不出问题啦
      

  32.   


    这个我就不试啦, 直接说我的结论了:
    “确定要关闭该页面吗?”这个提示窗口会弹出来, 但无论你是确认,还是取消; 页面都会关闭的。
    onbeforeunload 只有 return 一个字符串时,才会起到效果。
      

  33.   


    都说取消什么不做了  还会执行session清空代码吗
      
      

  34.   

    看了你 56 楼的代码, 没哪个地方 可以阻止程序关闭的哦~
    60楼的代码,confirm起不到作用,最终页面都会关闭
      

  35.   

    用window.onbeforeunload = function() 

      if(confirm('确定要关闭该页面吗?')) 
          { 
              window.location.href="loginOut.aspx"; 
                        sleep(1000);       } 
    } 重定义的这个方法无论confirm结果返回如何,它都要执行onunload事件,所以页面最终都是要关闭的
      

  36.   

    如果能找到一个让onunload事件失效的方法,那么问题就解决了
      

  37.   


    这个 就是我郁闷的原因啦;
    在执行 onbeforeunload 之后,如果能够有个过渡的东西, 然后再执行 onunload 就好了;
    onbeforeunload 只能给出提示, 不能在这个方法中处理。
    在 onunload 中处理也不行, 没辙了哎~~~