我想在关闭页面时候,触发事件去做一些检查工作,如果条件符合,则可以保存资料,关闭页面;如果条件不符合,则给出提示并且不关闭页面。小弟是新手,请高手们指点一下!

解决方案 »

  1.   

    beforeunload 或 unload 事件
      

  2.   

    <form id="form1" runat="server" onunload=...>
      

  3.   

    下面是MSDN的代码,你可以在closeIt里面再做自己的处理。<HTML>
    <HEAD>
    <SCRIPT>
    function closeIt()
      {
        event.returnValue = "Any string value here forces a dialog box to \
                            appear before closing the window.";
      }
    </SCRIPT>
    </HEAD>
    <BODY onbeforeunload="closeIt()">
        <a href="http://www.microsoft.com">Click here to navigate to 
            www.microsoft.com</a>
    </BODY>
    </HTML>
      

  4.   

    function closeFun()
        {
            if(event.clientY<0||event.altKey==true)
            {
                var xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                xmlHttp.open("get","OnClose.aspx",false);
                xmlHttp.send(null);
            }
        }
        window.onbeforeunload=closeFun;
      

  5.   

    function   close() 
            { 
                //............
             } 
           如上,先把写好的JS函数close()传到客户端,由<BODY onbeforeunload="close()">关闭页面前调用close()
      

  6.   

    onbeforeunload刷新时也触发呀。