Chapter II = "Using JavaScript in HTML

Source: Internet
Author: User

Using JavaScript in HTML

2.1 <script> Elements

2.1.1 The properties of the script element

Async: Optional. Indicates that the script should be downloaded immediately, but should not interfere with other actions on the page. Valid only for external scripts.

CharSet: Optional. Represents the character set of the code specified by the SRC attribute.

Defer: Optional. Indicates that the script can be deferred until the document is fully parsed and displayed before execution. Valid only for external scripts.

SRC: Optional. Represents the external file that contains the code to execute

Type: Optional. Represents the content type of the scripting language in which the code is written.

  2.1.2 Explanation Order

A. The JS code contained within the <script> element will be interpreted from top to bottom, and then stored in its own environment. Until all code inside the <script> element is evaluated by the interpreter, the rest of the page will not be loaded or displayed by the browser.

B. If you want to include an external JS file through the <script> element, then the SRC attribute is required. When parsing an external JS file, the processing of the page will also be temporarily stopped.

2.1.3 The location of the label

Modern Web applications typically put all of the JS references behind the pages in the <body> element, so that the content of the page is fully present in the browser before parsing the included JS code. The user will also feel the speed of opening the page because of the shortening of the window display blank time.

2.1.4 Delay Script

The Defer property applies only to external script files and causes the script to be deferred until the entire page has been resolved before running. Setting the Defer property in the <script> element is equivalent to telling the browser to download it now, but defer execution.

In reality, deferred scripts are not necessarily executed in sequence or in front of the domcontentloaded event, so it is best to include only one deferred script.

2.1.5 Asynchronous Script

Async applies only to external script files and tells the browser to download the file immediately. Unlike defer, however, scripts marked as Async are not guaranteed to be executed in the order in which they are specified. So make sure that multiple asynchronous scripts are not dependent on each other.

The purpose of specifying the async attribute is to not allow the page to wait for two scripts to download and execute, thus loading the page's other content asynchronously. For this reason, it is recommended that asynchronous scripts do not modify the DOM during loading. Asynchronous scripts must be executed before the Load event of the page, but may be executed before or after the domcontentloaded event

2.2 Choosing an embedded code or an external file

The best practice is to use external files to include JS code whenever possible, and the advantages of using external files

    1. Maintainability
    2. Cacheable: The browser is able to cache all external JS code connected according to the specific settings. That is, if you have two pages with the same file, the file needs to be downloaded only once. Therefore, the end result is the ability to speed up page loading.
    3. Adapting to the future: Use external files to include JS without using XHTML or annotations hack

2.3 Document Mode

IE5.5 introduces the concept of document mode, the first two document modes are: Promiscuous mode and standard mode. Promiscuous mode makes IE behave the same as IE5 (including non-standard features), while the standard mode allows IE to behave more closely to standard behavior. Although these two modes mainly affect the rendering of CSS content, but also affect the interpretation of JS execution.

If the document type declaration is not found at the beginning of the document, all browsers turn on promiscuous mode by default. But using promiscuous mode is not a recommended practice, because different browsers behave differently in this mode, and if you don't use some hack technology, cross-browser behavior is simply not consistent.

So you should use standard mode

<! DOCTYPE html>

2.4 Summary

Insert JS into the HTML page to use the <script> element. This element can be used to embed JS into an HTML page, to mix the script with the tag, or to include the external JS file.

When you include an external JS file, you must set the SRC attribute to a URL that points to the appropriate file. This file can be either a file located on the same server as its page or a file in any other domain.

Without using the defer and async attributes, all <script> elements are parsed in the order in which they appear in the page

Use the Defer property to have the script execute after the document is fully rendered. Deferred scripts are always executed in the order in which they are specified.

Using the Async property indicates that the current script is no more than waiting for other scripts and does not have to block document rendering. Asynchronous scripts cannot be guaranteed to execute in the order in which they appear in the page.

Chapter II = "Using JavaScript in HTML

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.