2.1 <script> Elements
The main way to insert JavaScript into an HTML page is to use the <script> element.
HTML 4.01 defines the following 6 properties for <script>: (optional)
Async: Download the script now without interfering with other actions on the page, only valid for external script files.
CharSet: The character set of the code specified by the SRC attribute. Ignored by most browsers and seldom used.
Defer: Delay script to fully parse and display the document before execution, only valid for external script files.
SRC: Represents the external file that contains the code to execute.
Type: The content Type (MIME type) that represents the scripting language in which the code is written, Type=text/javascript.
There are two ways to use the <script> element:
Embed JavaScript code directly in the page, simply specify the type attribute for <script>, and then place the JavaScript code directly inside the <script> element, and the JavaScript code will be interpreted from top to bottom The "</script>" string cannot appear in the code and needs to be split into "<\/script>".
Using the <script> element to include external JavaScript files, the value of the SRC attribute points to a link to an external JavaScript file, which can be a file on the same server as the page containing it, or it can be a file in any other domain External file with a. js extension.
2.1.1 The location of the label
Generally, all JavaScript references are placed in front of the,</body> tag behind the contents of the page in the <body> element.
2.1.2 Delay Script
Set the Defer property in the <script> element, download now but defer execution, preferably containing only one delay script.
2.1.3 Asynchronous script
Async scripts are not guaranteed to be executed in the order in which they are specified, so make sure that the scripts are not dependent on each other.
2.1.4 Use in XHTML (skipped) 2.1.5 deprecated syntax
In browsers that do not support JavaScript, include the JavaScript code in an HTML comment <!---->.
2.2 Embed code with external files
It is a best practice to use external files to contain JavaScript code whenever possible. The advantages are: maintainability, cacheable, adaptable to the future.
2.3 Document Mode
Promiscuous mode, Standard mode, quasi-standard mode
<! DOCTYPE HTML>//HTML5 Document Type declaration
2.4 <noscript> Elements
When a browser does not support JavaScript, the page is degraded smoothly, except for any HTML element that can appear in <body>,<script>.
Chapter II using JavaScript in HTML