As long as the mention of putting JavaScript on a Web page, you have to involve the language core--html of the Web.
The main way to insert JavaScript into an HTML page is to use the <script> element.
HTML4.0.1 defines the following 6 properties for <script>:
- Async: Optional. Indicates that the script should be downloaded immediately, but should not interfere with other actions on the page, such as downloading additional resources or waiting for other scripts to load. Valid only for external script files.
- CharSet: Optional. Represents the character set of the code specified by the SRC attribute. Because most browsers ignore its value, this property is rarely used.
- Defer: Optional. Indicates that the script can be deferred until the document is fully parsed and displayed before execution. Valid only for external script files. IE7 and earlier versions of this property are also supported for embedded scripting.
- Language: Deprecated. Originally used to represent scripting languages (such as JavaScript, JavaScript1.2, or VBScript) used to write code. Most browsers ignore this property, so there's no need to use it anymore.
- SRC: Optional. Represents the external file that contains the code to execute.
- Type: Optional. Can be seen as an alternative attribute of language, a content type (also a MIME type) that represents the scripting language in which the code is written. Although both Text/javascript and Text/ecmascript have not been recommended, people have been using text/javascript for a long time. In fact, the MIME type used by the server when transmitting JavaScript files is usually application/x-javascript, but setting this value in type may cause the script to be ignored. In addition, the following values can be used in non-IE browsers: Application/javascript and Application/ecmascript. Given the conventional and maximum browser compatibility, the current value of the type attribute is still text/javascript. However, this property is not required, and if this property is not specified, the default value is still text/javascript.
There are two ways to use the <script> element:
- Embed JavaScript code directly in the page
- Include external JavaScript files
The JavaScript code contained within the <script> element will be interpreted from top to bottom. All code inside the interpreter execution <script> element will not be loaded or displayed by the browser until the job is completed.
The external file is loaded into the current page, and the processing of the page is temporarily stopped when parsing an external JavaScript file, including downloading the file.
(1) Embed JavaScript code directly in the page
When embedding JavaScript code with the <script> element, you only need to specify the Type property for <script>. Then, put the JavaScript code directly inside the element as follows:
<script type= "Text/javascript" > Alert ("Hello");</script>
When using <script> embedding JavaScript code, remember not to have the "</script>" string anywhere in your code. Because according to the rules of parsing embedded code, when the browser encounters the string "</script>", it is considered to be the end of the </script> tag, you can solve this problem by escaping the character "\":
<script type= "Text/javascript" > Alert ("<\/script>");</script>
(2) include external JavaScript files
The SRC attribute is required if you want to include an external JavaScript file through the <script> element. The value of this property is a link to an external JavaScript file. For example:
<script type= "Text/javascript" src= "Demo.js" ></script>
The <script> element with the SRC attribute should not contain additional JavaScript code between its <script> and </script> tags. If embedded code is included, only the external script file is downloaded and executed, and the embedded code is ignored.
Note: As a rule, external JavaScript files have a. js extension. However, this extension is not required because the browser does not check the extension of the file that contains the JavaScript. This makes it possible to dynamically generate JavaScript code using JSP, PHP, or other server-side languages. However, the server usually needs to look at the extension to determine which MIME type to apply to the response. If you do not use the. js extension, make sure that the server returns the correct MIME type.
(3) position of the label
As a traditional practice, all <script> elements should be placed in the
However, including all JavaScript files in the
For pages that require a lot of JavaScript code, this will undoubtedly cause the browser to have a noticeable delay in rendering the page, while the browser window in the delay period will be blank.
To avoid this problem, modern Web applications typically place all JavaScript references behind the page content in the <body> element, which is before the </body> end tag.
JavaScript Learning: How JavaScript is introduced