JavaScript Drop Location Differences
The script in the page executes immediately after the page is loaded into the browser. We don't always want that. Sometimes we want to execute the script when the page is loaded, and in the other case we want the script to execute when the user triggers the event.script
in the
head
section: The script is executed when the script is called, or when the event is triggered. When you put the script in the Head section, you can ensure that it is loaded before you need to use the script. ........
script
in the
body
section: The script is executed when the page is loaded. When you place the script in the body section, it generates the contents of the page. ....
in
the body
and
head
section of the script: you can place any number of scripts in the document, so you can either place the script in the body or place it in the head section. ........
using external
JavaScript Sometimes, you might want to run JavaScript on several pages without writing the same script on each page. for this purpose, you can write JavaScript into an external file. Then save the file with a. js suffix.
Note:external files cannot contain
In short:
- If you put it in the head, it will be loaded first when the page is initialized.
- If placed inside the body, and so on the page initialization completes, only then to load.
Reference: http://blog.sina.com.cn/s/blog_54c367d4010179ks.html
JavaScript Drop Location Differences