js Method for checking whether the browser is closed

  • 2021-07-07 06:32:19
  • OfStack

This article example shows how js checks whether to close the browser. Share it for your reference, as follows:


// Close browser prompts 
window.onbeforeunload = function (e) {
  e = e || window.event;
  var y = e.clientY;
  if (y <= 0// Click the Close button in the browser or tab 
  || y >= Math.max(document.body ? document.body.clientHeight : 0, document.documentElement ? document.documentElement.clientHeight : 0)// Click the Close button on the taskbar 
  ) {
    //IE  And  Firefox 
    e.returnValue = " Are you sure you want to refresh or close the browser window? ";
  }
  // Google 
  return " Are you sure you want to refresh or close the browser window? ";
}

More readers interested in JavaScript can check the topics of this site: "Summary of json Operation Skills in JavaScript", "Summary of JavaScript Switching Special Effects and Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Animation Special Effects and Skills", "Summary of JavaScript Error and Debugging Skills", "Summary of JavaScript Data Structure and Algorithm Skills", "Summary of JavaScript Traversal Algorithm and Skills" and "Summary of JavaScript Mathematical Operation Usage"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: