When doing some questions about online membership, we often need to determine whether the user is offline based on whether the browser is closed, and then remove the user from the session and application.
Because the browser is stateless, when the capture browser is closed, there will be two situations: 1. actually close the browser (. click CLOSE. B. right-click the taskbar and close c. press alt + F4) 2. refresh the browser. How can we identify and distinguish these two actions? I. javascript code processing method: the code is as follows: function window. onbeforeunload () {// you can click the close button in the upper-right corner of the browser or press alt + F4 to close if (event. clientX> document. body. clientWidth & event. clientY <0 | event. altKey) {// alert ("click Close"); document. getElementById ("hiddenForm: hiddenBtn "). click (); // window. event. returnValue = "are you sure you want to exit this page? ";}// Click the taskbar and right-click to close the task. S or press alt + F4 to disable else if (event. clientY> document. body. clientHeight | event. altKey) {// alert ("right-click to close the taskbar"); document. getElementById ("hiddenForm: hiddenBtn "). click (); // window. event. returnValue = "are you sure you want to exit this page? ";}// In other cases, refresh else {// alert (" Refresh page ") ;}} where event. clientX mouse cursor X coordinate document. body. clientWidth form workspace width event. clientY mouse cursor Y coordinate event. whether or not to press alt key 2. event capture method: the code is as follows: <body scroll = "no" onbeforeunload = "return CloseEvent ();" onunload = "UnLoadEvent () "> </body> <script language =" JavaScript "type =" text/javascript "> var DispClose = true; function CloseEvent () {if (DispClose) {return "do you want to leave the current page? ";}} Function UnLoadEvent () {DispClose = false; // handle the action before closing the page here} </script> the onbeforeunload event is triggered before the page is uninstalled, if "yes" is selected, the unload event is triggered when the page is uninstalled. Otherwise, no operation is performed on the returned page.