JS browser Close event

Source: Internet
Author: User

JS determines how to refresh, close tabs, and close browsers on pages.

In the past few days, I was wondering how to get the user's exit time when the user logs out and uses session to save the user's logon data and time ?? Because the user may not use the "exit" button on the page to trigger your write method of saving the user exit, but directly Alt + F4 to directly close the current tab, or close the browser directly. andThis function enables online duration statistics.

As we all knowProgramEmployees are faced with illegal operations for various 2B users. The boss's salary cannot meet their needs.

In addition, the global session_end event seems to be okay, but it is not stable and easy to use.

Effect:

Next, I started to use js to determine the user's operations. Then I used ajax to switch to the application page for data operations.

JS determines whether the user closes the browser, page tag, or refresh.

Jump to the application page with Ajax

On this application page, the process user exits from the data storage operation.

CodeNot hard .. at this time, you will find that the method does not work. when refreshing, you can enter the operation page, but when you close the browser, you will not execute the code on the Operation page. You can only find another method. Why not, this may be related to the page running mechanism. When the browser is closed, the main page first runs js code, and then jumps to ashx, the main page is closed. what should I do if I want to call the execution method in JS on the homepage to run the method for saving user information before closing the page ??

I suddenly thought of webserver. It seems like this is a good thing ~ I decided to try it out. I thought it would work ~

I believe that many programmers like me will want to solve this problem, so today I will share this code ~

There is also a problem with browser compatibility. the location of "close" is different for each browser. so this compatibility needs to be adjusted by the friends of the school. here I have debugged ie9 ..

The Code is as follows:

 

Web Service Page code:

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.