Home>

The principle is to detect the browser window size at this time by leaving the page behavior time onunload trigger time,Based on the size, the user is judged to be refreshed.Jump or close the program

code show as below

window.onunload=function () {
    var a_n=window.event.screenx-window.screenleft;
    var a_b=a_n>document.documentelement.scrollwidth-20;
    if (a_b&&window.event.clienty<0 || window.event.altkey) {
         alert ("Close page behavior");
    } else {
         alert ("Jump or refresh page behavior");
      }
}

Handy when using the close button in the top right corner of your browser,But closed on the tab and closed on the taskbar,This method will not work

The js tag has only the onloadonunloadonbeforeunload event, and no onclose event.

The onunload event is executed regardless of whether the page is closed or refreshed.

How to catch the page closing?

Perform only onload when the page loads

Only execute onunload when the page is closed

When the page refreshes, first execute onbeforeunload, then onunload, and finally onload.

This way we can add a mark in onbeforeunload,Judging the mark in onunload,You can reach to determine whether the page is really closed

More complete compatible ff

code show as below

<mce:script type="text/javascript"><!-
    function close (evt) //author:sunlei
    {
      var isie=document.all?true:false;
      evt=evt?evt:(window.event&window.event:null);
      if (isie) {//ie browser
        var n=evt.screenx-window.screenleft;
        var b=n>document.documentelement.scrollwidth-20;
        if (b&&evt.clienty<0 || evt.altkey) {
          //alert ("is closed but not refreshed");
          window.location.href="../include/logout.php";
        }
        else {
          //alert ("is refreshed instead of closed");
          return false;
        }
      }
      else {//Firefox browser
        if (document.documentelement.scrollwidth!=0)
        {
          //alert ("is refreshed instead of closed");
          //window.location.href="report_list.php?ss=1 ";
          return false;
        }
        else {
          alert ("is closed instead of refresh");
          //window.location.href="repost_list.php?ss=0 ";
          //alert ("bbbbbbb");
        }
      }
    }
//-></mce:script>
<body onunload="close (event);">

The above method can't judge the multi-tab browser,Such as 360, ie8, see below

code show as below

function closeopen (event) {
     if (event.clientx<= 0 || event.clienty<0) {
       //Get the current time
      var date=new date ();
      //Set date to elapsed time
      alert ("Close webpage");
      date.settime (date.gettime ()-10000);
      //Delete the userid cookie
      document.cookie="zhuangtao;expire =" + date.toutcstring ();
      document.cookie="quanxianzifucuan;expire =" + date.toutcstring ();
      document.cookie="quanxian;expire =" + date.toutcstring ();
     s0 +="Close window!";sw=1;
      onbeforeunload ();
      //window.event.returnvalue="Close the browser and exit the system.
";
     }
     else
     {
        alert ("Refresh or leave");
     }
 }
        var currentkeycode=-1;
  function document.onkeydown () {//All subordinate pages of this window must contain this function
    top.currentkeycode=event.keycode;
  }
  function onbeforeunload () {
   var sw=0, s0="";
    if (currentkeycode == 116)
    {
      s0 +="Refresh window! (f5)";
    }
    else
    {
      if ((event.altkey)&&(currentkeycode == 115))
      {
         s0 +="Close window! (alt + f4)";sw=1;
          //Get the current time
          var date=new date ();
          //Set date to elapsed time
         alert ("Close window");
          date.settime (date.gettime ()-10000);
          //Delete the 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 +="Refresh window!";
        }
        else
        {
        //Get the current time
          var date=new date ();
          //Set date to elapsed time
          alert ("Close webpage");
          date.settime (date.gettime ()-10000);
          //Delete the userid cookie
          document.cookie="zhuangtao;expire =" + date.toutcstring ();
          document.cookie="quanxianzifucuan;expire =" + date.toutcstring ();
          document.cookie="quanxian;expire =" + date.toutcstring ();
         s0 +="Close window!";sw=1;
        }
      }
    }
    if (sw == 1)
    {
     event.returnvalue="";
    }
    else
    {
     currentkeycode=-1;
    }
  }
<body onunload="closeopen (event)"></body></html>

The above is only disabled in the taskbar,Can basically meet our requirements.

The above is the JavaScript that this article introduces to everyone to determine whether the webpage is closed or refreshed.Hope you like it.

  • Previous C # class to control camera
  • Next Briefly explain the definition of C ++ internal and external functions and macros