SoFunction
Updated on 2025-04-10

Detailed explanation of the example of JS monitoring and closing browser operation

Detailed explanation of the example of JS monitoring and closing browser operation

The requirements are as follows:

The user closes the browser notification background.

The plan is as follows:

1. Use js monitoring to close the browser operation.

2. When the user closes the browser, sends ajax request to the background and performs corresponding business operations.

The code is as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:///TR/html4/"> 
<html> 
<head> 
 <title> New Document </title> 
 <meta name="Generator" content="EditPlus"> 
 <meta name="Author" content=""> 
 <meta name="Keywords" content=""> 
 <meta name="Description" content=""> 
</head> 
  
<body> 
 <script type="text/javascript"> 
  = onbeforeunload_handler;   
   = onunload_handler;   
  function onbeforeunload_handler(){   
    var warning="Confirm to close?";     
    //Your business operations.  .  .  .    return warning;   
  }   
     
  function onunload_handler(){   
    var warning="Thank you for coming";  
    //Your business operations.  .  .  .    alert(warning);   
  }   
 </script> 
</body> 
</html>

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!