JavaScript determines whether the page is closed or refreshed _javascript tips

Source: Internet
Author: User
Tags close page

The principle is that by leaving the page behavior time onunload trigger time to detect the window size of the browser at this time, according to the size of the user is to determine the refresh, jump or close the behavior of the program

The code is as follows

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 ');   
      } 

It works with the Close button in the upper-right corner of the browser, but it doesn't work when you close the tab and close it on the taskbar.

JS tags only onloadonunloadonbeforeunload events, and no onclose events.

The OnUnload event is performed regardless of whether the page is closed or refreshed.

How do I capture a page close?
The onload is only executed when the page loads
Only perform onunload when page closes
When the page is refreshed, execute onbeforeunload First, then onunload, and finally onload.

So we can add a tag in the onbeforeunload and judge the tag in the onunload to see if the page really shuts down.
More complete, compatible FF

The code is as follows

<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 rather than refreshed"); Window.location.href= ".
        /include/logout.php ";
          } else{//alert ("is refreshed rather than closed");
        return false;
          } else{//Firefox browser if (document.documentelement.scrollwidth!=0) {//alert ("is refreshed rather than closed");
          Window.location.href= "Report_list.php?ss=1";
          
        return false;
          else{alert ("is off rather than refreshed");
          Window.location.href= "Repost_list.php?ss=0";
        Alert ("BBBBBBB"); }}//--></mce:script> <body onunload= "Close" (event);"  >

The above method has no way to judge the browser of multiple tabs, such as 360,IE8, see below

The code is as follows

function Closeopen (event) {if (event.clientx<=0 | | event.clienty<0) {//Get current time Var date=new Dat
      E ();
      Set date to past time alert ("Close page");
      Date.settime (Date.gettime ()-10000);
      Delete the UserID this 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 browser will exit system. ' 
     else {alert ("Refresh or leave");

  } var currentkeycode =-1;
  function Document.onkeydown () {//All subordinate pages of this window must contain the functions Top.currentkeycode = Event.keycode;
    function onbeforeunload () {var sw = 0, S0 = ""; if (Currentkeycode = = 116) {s0 = "Refresh Window!" (
    F5) "; else {if ((Event.altkey) && (Currentkeycode = =) {S0 = "close the window!" ( ALT+F4) ";
          SW = 1;Gets the current time var date=new date ();
          Sets date to the past time alert ("Close window");
          Date.settime (Date.gettime ()-10000);
          Delete the UserID this 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 current time var date=new date ();
          Set date to past time alert ("Close page");
          Date.settime (Date.gettime ()-10000);
          Delete the UserID this 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> 

The above only can not be used in the taskbar closed, basic to meet our requirements.

The above content is this article to everybody introduced the JavaScript judgment webpage is closes or refreshes, hoped everybody likes.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.