Capture browser close and refresh events

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.