How to use JavaScript <script> tags in html

Source: Internet
Author: User

Text address: JavaScript in HTML <script> tags how to use the detailed

As long as a mention of putting JavaScript on a Web page, you have to involve the core language of the Web--html. In the initial development of JavaScript, one of the important issues Netscape to solve is how to make JavaScript coexist with HTML pages without affecting the rendering of those pages in other browsers. After trying, correcting, and arguing, the final decision is to add unified scripting support for the Web. Many practices in the early days of the web were preserved and formally incorporated into the HTML specification.

<script> elements

The main way to insert JavaScript into an HTML page is to use the <script> element. This element is created by Netscape and implemented first in the Netscape Navigator2. Later, this element was added to the formal HTML specification. HTML4.01 defines the following 6 properties for <scripth>.

    • 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 scripts.

    • 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 scripts.

    • Language: Deprecated.

    • 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 known as a MIME type) that represents the scripting language in which the code is written.

There are two ways to use the <script> element: embed JavaScript code directly in the page and include external JavaScript files.

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:

[JS]View PlainCopy
    1. <script type="Text/javascript" >
    2. function Sayhi () {
    3. Alert ("hi!");
    4. }
    5. </script>

The JavaScript code contained within the <script> element will be interpreted from top to bottom. Taking the previous example, the interpreter interprets the definition of a function and then saves the definition in its own environment. The rest of the page will not be loaded or displayed by the browser until the interpreter has evaluated all the code inside the <script> element.

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:

[JS]View PlainCopy
    1. <script type="Text/javascript" src="example.js" ></script>

In this example, the external file example.js will be loaded into the current page. The external file must contain only those JavaScript code that is normally placed at the beginning <script> and end </script> middle. As with the parsing of embedded JavaScript code, the processing of the page is temporarily stopped when parsing an external JavaScript file, including downloading the file. If you are in an XHTML document, you can omit the </script> tag that ends in the previous sample code, for example:

[JS]View PlainCopy
    1. <script type="Text/javascript" src="Example.js"/>

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 the. js extension is not applicable, make sure that the server responds to the correct MIME type.

How to use JavaScript <script> tags in html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.