1. Put it in the head
This method will load the js code before the page is loaded. In this case, we cannot get the web page elements in the head because the web page has not been loaded yet. See the example below, when we want to dynamically modify the button When displaying the value, the code in the head will report a Cannot
set property 'value' of null error. This error is obtained when debugging in chrome. In IE, an error will also be reported that 'document.getElementById(...)' is empty or not an object. Of course, if you put the js code in the body, there will be no problem.
[html]
2. Place it in the body
and execute the js scripts in the order of placement. For example, in the above example, if we put js before the element is loaded, an error will also be reported.
[html]
Tip: When the js file is relatively large, it is recommended to load the js file before
. This will not affect the loading speed of the web page. If it is placed in the head, it will Load the js file first.