您的位置:首页 > Web前端 > HTML

html页面的onunload、onbeforeunload

2013-11-05 23:48 399 查看
<script>

    function getBO()

    {

       ret = confirm("你是否要关闭窗口");

       if(ret)

       {

          ret1 = confirm("你是否要收藏页");

          if(ret1)

          {

            external.addFavorite('网址','收藏名称')

           }

          this.close();

       }
    }

    //必须写在script内,如果body onload =...    onbeforeunload=...,firefox好像不起作用

      window.onbeforeunload=function(){alert('before unload');} //ie firefox

    window.onunload=function(){alert('kk');}; //ie

</script>

附最终代码:

    //主窗体加载,需要记录在线标志。因为在主窗体可能刷新,可能关闭窗口。

    //如果刷新,此时也会执行beforeunload时导致用户离线,因此onload中要将用户置为在线

    window.onload=function()

    {

        //alert("onload");

        //关闭窗体时才退出登录;刷新按钮,不能退出

        $.ajax({

          type:"POST",

          url:"oa_sys_manage",

          data:"type=online",

          dataType:"",

          async:false,

          success:function(data){

              //alert('已重新登录!');

              ;//

          }

        });       

    }

    

    //主窗体关闭事件处理  退出登录

    window.onbeforeunload=function(event)  //ie firefox

    {

        var evt=event?event:window.event;

        //alert(evt);

        //alert('before unload');

        var isIE=document.all?true:false;

        if(isIE)

        {

            alert('ie beforeunload');

            var a_n = evt.screenX - window.screenLeft;

            //alert(a_n);

            var a_b = a_n >document.documentElement.scrollWidth-20;

            //alert(a_b);

            if(a_b && evt.clientY<0 || evt.altKey)

            {

                alert('关闭页面行为');

                //关闭窗体时才退出登录;刷新按钮,不能退出

                $.ajax({

                  type:"POST",

                  url:"oa_sys_manage",

                  data:"type=offline",

                  dataType:"",

                  async:false,

                  success:function(data){

                      //alert('已安全退出系统!');

                      ;//

                  }

                });           

            }

            else

            {

               //alert('跳转或者刷新页面行为');

            }   

        }

        else

        {

            alert('firefox beforeunload');

            //关闭窗体时才退出登录;刷新按钮,不能退出

            $.ajax({

              type:"POST",

              url:"oa_sys_manage",

              data:"type=offline",

              dataType:"",

              async:false,

              success:function(data){

                  //alert('已安全退出系统!');

                  ;//

              }

            });

        }     

    } 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: