1) Similarities and Differences
Similarities: onunload and onbeforeunload are both called during refresh or shutdown. You can specify them in the <script> script using window. onunload or in <body>.
Differences:
A) onbeforeunload is executed before onunload. It can also prevent onunload execution.
B) onbeforeunload is called when you are about to read a new page from the server. At this time, you have not started reading the page. nunload has read the new page to be loaded from the server, it is called when the current page is to be replaced.
C) onbeforeunload can prevent page updates and closures. But onunload cannot.
The execution sequence of onload, onbeforeunload, and onunload events:
When the page is opened: Only onload is executed
When the page is closed: onbeforeunload first, and then onunload
When refreshing the page: First onbeforeunload, then onunload, and then onload
2) onbeforeunload and onunload events
Triggered on:
A) Close the browser window.
B) when you go to other pages through the address bar or favorites
C) Click return, forward, and refresh. When one of the home pages is displayed
D) When you click a url to connect to another page
E) when calling any of the following events: click, document write, document open, document close, window close, window navigate, window NavigateAndFind, location replace, location reload, form submit.
F) when you use window open to open a page and pass the window name on this page to the page to open.
G.
H) when submitting a form with the specified action through the input type = "submit" button.
It can be used in the following elements:
BODY, FRAMESET, window
Browser support:
IE4 +/Win, Mozilla 1.7a +, Netscape 7.2 +, Firefox0.9 +
3) do not call onbeforeunload when refreshing the page
4) onbeforeunload blocks page updates and closures
Onbeforeunload = "return 'Do you want to leave this page';">