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?