The main way to add JavaScript to an HTML page is to use <SCRIPT>: Contains external JavaScript files and embeds JavaScript code directly into the page.
Properties of <script>
<script> include attribute: async means that the external script should be downloaded immediately, defer that the script can delay the external JS to the DOM document is completely parsed and displayed before execution, SRC introduces an external file, type represents the content type of the script (MIME type) text/ JavaScript;
<script> writing format
When </script> strings appear in <script> and </script>, the "\" escape character is used to escape "<\/script>"
<script src= "# #" ></script> this format in HTML, and <script src= "# #"/> is not, can not be interpreted correctly by the browser. In XHTML, you can use <script src= "# #"/>
<script src= "# #" ></script> this with src tag, the middle is no longer add JavaScript code.
(Src refers to the source of the file, for example: the IMG tag also has src; Yes, it can be the same server as the page, or not the same server (that is, a different domain JavaScript file)) (the HREF is the address of the connection file, the meaning of the connection)
Regardless of the code, as long as the defer and async properties are not present, the browser will parse them sequentially in the order that they appear in the <script> elements page.
<script> Label Location
1, the traditional <script> placed in the
So now in order to avoid this, the JavaScript reference is usually placed in the <body> element, the last side of the page element
2, defer attribute (Delay script) (external JS only)
The script is deferred until the entire page has been parsed and then run, telling the browser to download it immediately, but defer to the
HTML5 advocates delaying scripts to be executed sequentially, while scripts take precedence over domcontentloaded events, but the fact is not necessarily sequential. So it's best to put it in <body> at the end.
3,async Property (Asynchronous script) (external JS only)
Async scripts do not guarantee the order in which they are executed, and the purpose is to not allow the page to wait for the external JS script to download and execute, thus asynchronously loading the other content of the page.
Asynchronous scripts must be executed before the Load event, and the order in which the domcontentloaded events are executed is not necessarily.
Embed code and external JavaScript
Support, encourage the use of external JavaScript files (good maintainability; cacheable: Two pages need the same JS, cache once)
<noscript>
For displaying content in browsers that do not support JavaScript
Using JavaScript's <script> tags and attributes in html