JavaScript-based judgment on whether the browser is closed or refreshed (ultra-accurate) _ javascript skills

Source: Internet
Author: User
This article mainly describes how to determine whether the browser is closed or refreshed (ultra-accurate) based on JavaScript. if you need it, you can refer to the core content summarized in this article, I personally feel helpful to you. for details, refer to the following:

Only onload is executed when the page is loaded.
When the page is closed, only onunload is executed.
When the page is refreshed, execute onbeforeunload first, then onunload, and finally onload.

After verification, I came to the conclusion that:

// For ie, Google, 360:

// Only onload is executed during page loading.
// When the page is refreshed, the onbeforeunload event is executed before the refresh. when the new page is about to replace the old page, the onunload event and the last onload event are executed.
// When the page is closed, the onbeforeunload event is first followed by the onunload event.

// For Firefox:

// When the page is refreshed, only onunload is executed; when the page is closed, only the onbeforeunload event is executed.
So back to the question, how can we determine whether the browser is closed or refreshed? I tried it over times based on various online statements, but it was not successful. the various statements are as follows:

Window. onbeforeunload = function () // author: meizz {var n = window. event. screenX-window. screenLeft; var B = n> document.doc umentElement. scrollWidth-20; if (B & window. event. clientY <0 | window. event. altKey) {alert ("close rather than refresh"); window. event. returnValue = ""; // Here you can place the operation code you want to do} else {alert ("refresh rather than close") ;}} window. onbeforeunload = function () // author: meizz {var n = window. event. screenX-window. screenLeft; var B = n> document.doc umentElement. scrollWidth-20; if (B & window. event. clientY <0 | window. event. altKey) {alert ("close rather than refresh"); window. event. returnValue = ""; // Here you can place the operation code you want to do} else {alert ("refresh rather than close ");}}

And

Function CloseOpen (event) {if (event. clientX <= 0 & event. clientY <0) {alert ("close");} else {alert ("refresh or quit");} script

..........................

These methods do not work, but I did not give up. I want to think ........

According to my conclusion above,

// For ie, Google, 360:

// Only onload is executed during page loading.
// When the page is refreshed, the onbeforeunload event is executed before the refresh. when the new page is about to replace the old page, the onunload event and the last onload event are executed.
// When the page is closed, the onbeforeunload event is first followed by the onunload event.

// For Firefox:

// When the page is refreshed, only onunload is executed; when the page is closed, only the onbeforeunload event is executed.
The onbeforeunload event is first refreshed, and then the server requests data. when the new page is about to replace the old page, the onunload event is taken. when the page is closed, the onbeforeunload event is first performed, and then the onunload event is immediately taken. In this case, the time between onbeforeunload and onunload is certainly longer than the time when it is disabled. this is true after testing.

Paste my test code:

Var _ beforeUnload_time = 0, _ gap_time = 0; var is_fireFox = navigator. userAgent. indexOf ("Firefox")>-1; // whether it is a Firefox browser window. onunload = function () {_ gap_time = new Date (). getTime ()-_ beforeUnload_time; if (_ gap_time <= 5) $. post (pathName + "/back/bi! Aaaa. s2? T = "+ _ beforeUnload_time, {msg:" close the browser ", time: _ gap_time}, function (json) {}," text "); else $. post (pathName + "/back/bi! Aaaa. s2? T = "+ _ beforeUnload_time, {msg:" browser refresh ", time: _ gap_time}, function (json) {}," text ");} window. onbeforeunload = function () {_ beforeUnload_time = new Date (). getTime (); if (is_fireFox) // close Firefox and execute $. post (pathName + "/back/bi! Aaaa. s2? T = "+ _ beforeUnload_time, {msg:" Firefox disabled "}, function (json) {}," text ");};

Server code (implemented by SSH ):

Public void aaaa () {System. out. println (base. getParameter ("msg") + ", interval:" + base. getParameter ("time "));}

For if (_ gap_time <= 5), 5 here is my preset. it depends on the client browser and is also related to the client machine configuration, when my machine closes the browser, the data interval between the onbeforeunload event and the onunload event cannot exceed 2 ms, and the interval between the refresh event 100% is greater than 2 ms because the server is accessed. The following shows my test results:

Next we will introduce the browser listener closing event and determine whether to refresh or close it.

You can use onunload or onbeforeunload to listen to closed events in the browser, but you cannot distinguish between closed events and refresh events. The following js code can partially listen to events that close the browser!

// Horizontal position of the mouse relative to the user's screen-horizontal position of the upper left corner of the window relative to the upper left corner of the screen = horizontal position of the mouse over the current window var n = window. event. screenX-window. screenLeft; // when the mouse is in the current window, n
 
  
M and B are true. 20 This value refers to the width of the close button var B = n> document.doc umentElement. scrollWidth-20; // window when the mouse is in the customer zone. event. clientY> 0; window. event. clientY <0if (B & window. event. clientY <0 | window. event. altKey | window. event. ctrlKey) {what you want to do when you close the browser} else if (event. clientY> document. body. clientHeight | event. altKey) {what you want to do when you close the browser}
 

This section of js allows you to listen to the browser close button, right-click the browser status bar, right-click the button in the pop-up menu, and various shortcut keys. However, you cannot monitor the browser by double-clicking the "browser" icon to close the browser or close the tab.

The above is all the content of this article. if you are not well-written, please give us more valuable comments.

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.