Onbeforeunload and onunload

Source: Internet
Author: User

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';">

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.