How to use JavaScript in HTML

Statement: All of my blogs are personal notes, used as personal knowledge index, so there are logic problems in the narrative, the span is large, I hope to understand. Share out for everyone to learn to read, if there is a mistake hope to point out, thank you!

< script> Tag properties:
Async: Optional, indicating that the script should be downloaded immediately but should not interfere with other operations on the page
Defer: Optional, which means that the script can be deferred until the document is fully parsed and displayed, and is valid only for external scripts
SRC: Optional, representing the external file that contains the code to execute
Type: Required, representing the content type of the scripting language in which the code is written, typically text/javascript
Use external JavaScript files
In the use of & lt; script> When embedding external JavaScript code, do not appear anywhere in the code. /script>
If you want to pass < script> Element to contain an external JavaScript file, then SRC is required. SRC points to a link to an external JavaScript file, either relative or absolute
The processing of the page also stops temporarily when downloading and parsing external JavaScript files
& LT with SRC attribute; script> JavaScript code should no longer be included between tags, and even if it was, it would be ignored
External JavaScript files carry a.js extension that can be ignored, but the server must return the correct MIME type
Label location
Placed in & lt; body> At the end of the
Because: The browser processes the HTML code sequentially, if placed at the beginning will cause the browser to first download and process the JavaScript external files, thus causing the web page to be placed in the < head> And add defer= “defer”

<script type="text/javascript" defer="defer" src="xxx.js"></script>

The because: Defer property causes the script to be deferred until the entire page has been loaded, called the defer script, which is best left with only one placed at < head> In, and add Async

<script type="text/javascript" async src="xxx.js"></script>

The because: Async attribute instructs the browser to download the script file immediately, but it should not prevent other actions on the page marked async from ensuring that the script is executed in the order in which it is recommended to place the script at < body> The end of the method


Read More: