Determine whether the browser is closed or refreshed (ultra-accurate) based on JavaScript. The javascript Browser

Source: Internet
Author: User

Determine whether the browser is closed or refreshed (ultra-accurate) based on JavaScript. The javascript Browser

This article is a summary of some of the core content, I personally feel helpful to everyone, the specific content can be seen below:

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> <body onunload = "CloseOpen (event) ">

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

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, B is false; when the mouse is outside the current window, n> m, B is 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.

Articles you may be interested in:
  • JavaScript code used to intelligently determine whether the browser is closed or refreshed
  • JavaScript code used to determine the horizontal and vertical screens and to disable the browser slide entries
  • Javascript obtains and judges the height and width of browser windows, screens, and webpages.
  • Js monitoring IE Firefox browser close, refresh, rollback, and forward events
  • A simple section of js to determine the browser kernel
  • JS identifies the access browser and determines whether it is android or ios
  • Summary of js/jquery browser judgment methods
  • JS determines whether the browser supports a certain CSS3 attribute.
  • Encapsulated js judgment operating system and browser code sharing
  • Use js to determine whether it is the 360 browser implementation code
  • How to determine the browser version and the browser kernel using js
  • JS method for determining whether the 360 secure browser's fast kernel is used

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.