Home > Web Front-end > JS Tutorial > Implementation code for judging page closing and page refresh in JavaScript_javascript skills

Implementation code for judging page closing and page refresh in JavaScript_javascript skills

WBOY
Release: 2016-05-16 16:38:38
Original
1254 people have browsed it

Today, because the project needs to determine whether the user’s behavior is to close or refresh when leaving the page
Although there is no direct method, it can be done with certain techniques
I have to lament the power of JavaScript! !
Please take a look at the code:

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


In fact, it is to detect the browser window size at this time through the onunload trigger time of leaving the page behavior time, and based on the size, it can be judged whether the user refreshes, jumps or closes the behavior.

Compatible with all major browsers!
Related labels:
source: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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template