JavaScript to determine the page close page refresh implementation code

  • 2020-03-30 03:46:14
  • OfStack

Today, because the project needs to determine whether the user's behavior is closed or refreshed when they leave the page
There is no direct way, but it can be done with some skill
JavaScript is so powerful!!
Take a look at the code:

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(' Closing page behavior ');
                  alert(' Jump or refresh page behavior ');    

It detects the window size of the browser at that time by the time the page behavior is left. The window size determines whether the user is a refresh, jump, or close behavior.

Compatible with major browsers!

Related articles: