Js实现360浏览器判断刷新或关闭

这段代码展示了如何使用JavaScript监听浏览器的刷新和关闭事件。通过onbeforeunload和onunload事件,结合键盘按键和鼠标位置来判断用户是刷新页面还是关闭浏览器。在用户关闭浏览器时,会弹出提示,并清除特定的cookies。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



function CloseOpen(event) { 
         if(event.clientX<=0 || event.clientY<0) { 
             //获取当前时间
           var date=new Date();
           //将date设置为过去的时间
           alert("关闭网页");
           date.setTime(date.getTime()-10000);
           //将userId这个cookie删除
           document.cookie="zhuangtao;expire="+date.toUTCString();
           document.cookie="quanxianzifucuan;expire="+date.toUTCString();
           document.cookie="quanxian;expire="+date.toUTCString();
          s0 += "关闭窗口!"; sw = 1;
            onbeforeunload();
           // window.event.returnValue = '关闭浏览器将退出系统.';
         } 
         else 
         { 
                alert("刷新或离开"); 
         } 
  } 
              
                var currentKeyCode = -1;


    function document.onkeydown() { // 本窗口的所有下属页面都必须含有本函数


       top.currentKeyCode = event.keyCode;
    }


    function onbeforeunload(){
    
     var sw = 0, s0 = "";
        if (currentKeyCode == 116) 
        {
           s0 += "刷新窗口!(F5)";
        }
        else 
        {
           if ((event.altKey)&&(currentKeyCode == 115)) 
            {
                  s0 += "关闭窗口!(alt+F4)"; sw = 1;
                   //获取当前时间
                   var date=new Date();
                   //将date设置为过去的时间
                  alert("关闭窗口");
                   date.setTime(date.getTime()-10000);
                   //将userId这个cookie删除
                   document.cookie="zhuangtao;expire="+date.toUTCString();
                   document.cookie="quanxianzifucuan;expire="+date.toUTCString();
                   document.cookie="quanxian;expire="+date.toUTCString();
            }
            else 
            {
               if ((event.clientX > 0)&&(event.clientX < document.body.clientWidth)) 
               {
                  s0 += "刷新窗口!";
               }
               else 
               {
                //获取当前时间
                   var date=new Date();
                   //将date设置为过去的时间
                   alert("关闭网页");
                   date.setTime(date.getTime()-10000);
                   //将userId这个cookie删除
                   document.cookie="zhuangtao;expire="+date.toUTCString();
                   document.cookie="quanxianzifucuan;expire="+date.toUTCString();
                   document.cookie="quanxian;expire="+date.toUTCString();
                  s0 += "关闭窗口!"; sw = 1;
               }
            }
        }
        if (sw == 1) 
        {
          event.returnValue = "";
        }
        else 
        {
          currentKeyCode = -1;
        }
    
    }


<body  onunload="CloseOpen(event)" ></body></html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值