This example summarizes the JS listening method for browser window closing events. Share it for your reference, as follows:
Method 1: (Applicable to IE browser, and there is no prompt for refreshing, only prompt when clicking the browser close button)
<script type="text/javascript">window.onbeforeunload=onclose;function onclose(){if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey){return "Are you leaving?";}}</script>Method 2: Applicable to IE and FF, without distinguishing between refresh and closing
<script type="text/javascript"> window.onbeforeunload = onbeforeunload_handler; window.onunload = onunload_handler; function onbeforeunload_handler(){ var warning="confirm exit?"; return warning; } function onunload_handler(){ var warning="Thank you for coming"; alert(warning); }</script>Method 3: Applicable to IE and FF, without distinguishing between refresh and closing, the easiest
<script type="text/javascript">window.onbeforeunload=onclose;function onclose(){return "Are you sure to exit?";}</script>Method 4: Applicable to IE and FF, without distinguishing between refresh and closing, slightly complicated
<script language="javascript">var MSG_UNLOAD="If you leave the archive system at this time, all the operation information will be lost. Do you leave?";var UnloadConfirm = {};//Enable the method of listening to browser refresh and close UnloadConfirm.set = function(confirm_msg){ window.onbeforeunload = function(event){ event = event || window.event; event.returnValue = confirm_msg; }}//Change the method of listening to browser refresh and close UnloadConfirm.clear = function(){ window.onbeforeunload = function(){};}UnloadConfirm.set(MSG_UNLOAD);</script>Method 5: Only suitable for closing buttons and shortcut keys under IE6, refresh without prompts
<script type="text/javascript">window.onbeforeunload=onclose;function onclose(){var warning = '<fmt:message key="systemMessage.exitWarning" />';var beforeExit='<fmt:message key="systemMessage.beforeExitWarning" />'; if(event.clientY<0 && event.clientX>document.body.clientWidth-20 || event.clientY<0 && event.clientX<20 || event.altKey || event.ctrlKey || event.clientY>document.body.clientHeight){alert(beforExit);return warning;}}</script>Also, JS to determine browser type
<script type="text/javascript"> var Sys = {}; var ua = navigator.userAgent.toLowerCase(); if (window.ActiveXObject) Sys.ie = ua.match(/msie ([/d.]+)/)[1] else if (document.getBoxObjectFor) Sys.firefox = ua.match(/firefox//([/d.]+)/)[1] else if (window.MessageEvent && !document.getBoxObjectFor) Sys.chrome = ua.match(/chrome//([/d.]+)/)[1] else if (window.opera) Sys.opera = ua.match(/opera.([/d.]+)/)[1] else if (window.openDatabase) Sys.safari = ua.match(/version//([/d.]+)/)[1]; //Test the following if(Sys.ie) document.write('IE: '+Sys.ie); if(Sys.firefox) document.write('Firefox: '+Sys.firefox); if(Sys.chrome) document.write('Chrome: '+Sys.chrome); if(Sys.opera) document.write('Opera: '+Sys.opera); if(Sys.safari) document.write('Safari: '+Sys.safari);</script>Differentiate between browsers, IE and FF are handled separately (strangely, IE sometimes fails)
<script type="text/javascript">window.onbeforeunload=onclose;function onclose(){var Sys = {};var warning = '<fmt:message key="systemMessage.exitWarning" />';var ua = navigator.userAgent.toLowerCase();if (window.ActiveXObject) Sys.ie = ua.match(/msie ([/d.]+)/)[1]else if (document.getBoxObjectFor) Sys.firefox = ua.match(/firefox//([/d.]+)/)[1]if(Sys.ie) {//for IEif(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey){window.event.returnValue = warning ;}}if(Sys.firefox) //for FFreturn warning;}</script>The easiest way to judge browser type
<script type="text/javascript">if(-[1,]){ alert("This is not IE browser!");}else{ alert("This is IE browser!");}</script>[1,] In the standard browser, the string "1" will be returned, which is equivalent to calling [1,].toString,
, IE returns "1,". However, IE and the standard will pass the detection, so use a negative sign to cast it into a number.
The standard can be successfully converted to 1, and 1 will automatically convert to true in if, while IE will convert to NaN and then automatically convert to false!
For more information about JavaScript related content, please check out the topics of this site: "Summary of JSON operation techniques in JavaScript", "Summary of JavaScript switching effects and techniques", "Summary of JavaScript search algorithm techniques", "Summary of JavaScript animation effects and techniques", "Summary of JavaScript errors and debugging techniques", "Summary of JavaScript data structures and algorithm techniques", "Summary of JavaScript traversal algorithms and techniques" and "Summary of JavaScript mathematical operations usage"
I hope this article will be helpful to everyone's JavaScript programming.