Home Web Front-end JS Tutorial JavaScript determines whether the web page is closed or refreshed_javascript skills

JavaScript determines whether the web page is closed or refreshed_javascript skills

May 16, 2016 pm 03:39 PM
javascript Web page

The principle is to detect the browser window size at this time through the onunload trigger time of leaving the page behavior time. Based on the size, it can be judged whether the user refreshes, jumps or closes the behavior 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('关闭页面行为'); 
    }else{ 
         alert('跳转或者刷新页面行为');   
      } 
}
Copy after login

It works well when using the close button in the upper right corner of the browser, but it doesn’t work when closing on a tab or closing on the taskbar.

js tag only has onloadonunloadonbeforeunload event but no onclose event.

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

How to capture the page closing?
Onload is executed when the page is loaded
Onunload is executed only when the page is closed
Onbeforeunload is executed first, then onunload, and finally onload is executed when the page is refreshed.

In this way we can add a mark in onbeforeunload and judge the mark in onunload to determine whether the page is really closed
More complete compatibility with ff

The code is as follows

 document.documentElement.scrollWidth-20;
        if(b && evt.clientY
Copy after login

The above method cannot judge multi-tab browsers, such as 360, ie8, etc. Let’s look at the

code below As follows

function CloseOpen(event) { 
     if(event.clientX<=0 || event.clientY0)&&(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;
    }
  
  }
Copy after login

The above only cannot be used and the taskbar is closed, which basically meets our requirements.

The above content is the javascript introduced in this article to determine whether the web page is closed or refreshed. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to send web pages to desktop as shortcut in Edge browser? How to send web pages to desktop as shortcut in Edge browser? Mar 14, 2024 pm 05:22 PM

How to send web pages to desktop as shortcut in Edge browser?

Possible reasons why the network connection is normal but the browser cannot access the web page Possible reasons why the network connection is normal but the browser cannot access the web page Feb 19, 2024 pm 03:45 PM

Possible reasons why the network connection is normal but the browser cannot access the web page

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to implement an online speech recognition system using WebSocket and JavaScript

What to do if the webpage cannot be opened What to do if the webpage cannot be opened Feb 21, 2024 am 10:24 AM

What to do if the webpage cannot be opened

What should I do if the images on the webpage cannot be loaded? 6 solutions What should I do if the images on the webpage cannot be loaded? 6 solutions Mar 15, 2024 am 10:30 AM

What should I do if the images on the webpage cannot be loaded? 6 solutions

How to open php on the web page How to open php on the web page Mar 22, 2024 pm 03:20 PM

How to open php on the web page

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

How to use JavaScript and WebSocket to implement a real-time online ordering system

See all articles