Which part of the HTML JavaScript will be executed when the page is loaded?

Source: Internet
Author: User

Recently encountered a problem:
Which part of the HTML JavaScript will be executed when the page is loaded ()
A: File Header

B: End of File

C:

D: <body> Label section

Although we all know the choice of D, but the answer to the explanation of the opinions, we can see that the browser load parsing rendering is not enough to understand.

The browser is loaded and parsed from top to bottom.

When the browser loads the html file and resolves to

What is this for?

Reason: JS has the possibility to modify the DOM, the most classic document.write, which means that before the completion of JS execution, all subsequent downloads of resources may not be necessary, which is the root cause of JS blocking the subsequent download of resources.

Example code:

<! DOCTYPE html>

Output:

This is Head JS
This is Test JS
This is Body JS
This is onload JS

This shows that head and head introduced script scripts are loaded and executed first, and OnLoad is executed last, so elements in <body> and </body> are executed when the page loads.

What's the use of knowing this?

If you put the DOM operation in the

So it's best to put <script> before </body>.

Which part of the HTML JavaScript will be executed when the page is loaded?

Related Article

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.