In a Web page, we can place JavaScript code anywhere in the HTML file, but generally in the head or body tag.
In general, where the,<script> elements are closely related to their functional role, here are 2 things to discuss:
1, put in
The <script> element is placed in the head in order for the browser to read the,<script> element in the first place, which will load execution when the entire page is first parsed, after the <title> element.
Then parse the rendering down in turn.
Applications: For example, the initialization of the page display JS must be placed in the head inside.
2, put in <body> part
The browser parses in the order of the page labels and executes the statement when it reads to the JavaScript code.
But some JS function is called through the event, so the specific place in the page which does not affect the role of the time, so, in consideration of the front-end performance problems, you can not be the first to execute and event calls JS code placed in the bottom of the body.
Write a look:
As noted above, JavaScript label location is closely related to function, I hope this article is helpful to everyone.