This article is some core content summarized by the editor. I personally think it will be helpful to everyone. Please see below for the specific content:
Onload is only executed when the page loads
Only execute onunload
when the page is closed
When the page is refreshed, onbeforeunload is executed first, then onunload, and finally onload.
After verification, I came to the conclusion:
//For IE, Google, 360:
//Onload is only executed when the page is loaded
//When the page is refreshed, the onbeforeunload event is executed before the refresh, the onunload event is executed when the new page is about to replace the old page, and finally the onload event is executed.
//When the page is closed, the onbeforeunload event occurs first, and then the onunload event.
//For Firefox:
//When the page is refreshed, only onunload is executed; when the page is closed, only onbeforeunload event is executed
So back to the topic, how to determine whether the browser is closed or refreshed? I have tried hundreds of times according to various opinions on the Internet without success. The various opinions are as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
and
1 2 3 4 5 6 7 8 9 10 11 |
|
........................
None of these methods worked, but I didn’t give up, thinking and thinking...
According to the above I came to the conclusion,
//For IE, Google, 360:
//Onload is only executed when the page is loaded
//When the page is refreshed, the onbeforeunload event is executed before the refresh, the onunload event is executed when the new page is about to replace the old page, and finally the onload event is executed.
//When the page is closed, the onbeforeunload event occurs first, and then the onunload event.
//For Firefox:
//When the page is refreshed, only onunload is executed; when the page is closed, only onbeforeunload event is executed
When refreshing, first onbeforeunload, and then get the server request data. When the new page is about to replace the old page, the onunload event occurs. When the page is closed, the onbeforeunload event occurs first, and then the onunload event immediately. Then when refreshing, the time between onbeforeunload and onunload is definitely longer than when closing, and this is indeed the case after testing.
Posted my test code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Server code (SSH implementation):
1 2 3 |
|
For if(_gap_time <= 5), the 5 here is my default, which depends on the client browser and is also related to the client's machine configuration. When my machine closes the browser, the onbeforeunload event is the same as The data interval of the onunload event does not exceed 2ms, and the interval during refresh is 100% greater than 2ms because the server needs to be accessed. My test results are posted below:
The following will introduce the browser to close the listening event and determine whether to refresh or close
Using onunload or onbeforeunload can monitor the browser closing event, but it is impossible to distinguish between closing and refreshing. The following js code can partially monitor the event of closing the browser!
1 2 3 4 5 6 7 8 9 10 |
|
This piece of js can monitor the mouse click on the browser close button, the right click of the mouse on the browser status bar to close the pop-up menu, and various shortcut keys. However, double-clicking the icon in the upper corner of the browser to close the browser and close the tab cannot be monitored.
The above is the entire content of this article. If it is not well written, please give me your valuable opinions.