Judge the user's online status onbeforeunload event _javascript skills

Source: Internet
Author: User
Get user Login State Needless to say, judge to leave the words have a little problem, if the user is in accordance with the designer's rules trigger the exit of the event that there is no difficulty, but the user to leave a variety of ways, how the user illegally leave the time of the decision to leave it? The most common illegal departure is to close the browser.
Copy Code code as follows:

<body onbeforeunload= "Body_onunload ()" >
<script>
Window.onbeforeunload = function () {
if (window.event.clientx>document.body.clientwidth&&event.clienty<0| | Event.altkey)
{
Alert ("Browser shutdown");
}
Else
{
Alert ("Refresh or jump to another page");
}
}
</script>
</BODY>

The above is a fragment, the body of the unload and onbeforeunload events will be triggered when the browser clears the loaded information, which means that the page will trigger when loopback, redirect, or shut down. This event, together with JavaScript processing, can be used to illegally close the browser and count online numbers.

But there is a problem, how to determine whether the user is closed or refreshed, loopback, redirect it?

Window.event.clientX and Window.event.clientY will capture the mouse relative to the window's desktop coordinates when the current event occurs, usually IE's close button will be in the upper-right section of the page, So when the point is closed, the mouse coordinates of the y-coordinate must be less than 0, in addition, the mouse coordinates in the x direction of the coordinate value will be greater than the width of the form, so from these two conditions can determine whether the mouse is in the point of the button off the Onbeforeload event.
There is also a closure method is Alt+f4, through the Event.altkey can be judged, the event occurs when the ALT key is not pressed, this also determines whether the time alt+f4 to close the window.

However, there is also a problem, when the use of some special left theme when the Close button can trigger the number of coordinates not necessarily less than the width of the form, so the above example of the window.event.clientx> Document.body.clientWidth This condition can not be.

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.