What is the difference between <script> elements in different positions

1. The js code in the HTMLbody will be executed when the page is loaded. Generally, the code executed when the page is loaded is placed in the body

2. The js code in the HTML head will be pre-loaded, but it will be executed when it is called. Generally, the code that needs to be called or the code that is triggered by the event will be executed.

Placed in the head, external code files are generally referenced in the head to ensure that the external file is loaded when the page is loaded, which can improve efficiency when executed

Guess you like

Origin www.cnblogs.com/1521681359qqcom/p/12190329.html