"Reading" "Elevation 3" ch2--using JavaScript in HTML

Source: Internet
Author: User

Using JavaScript in HTML
<script> elements

The main way to insert JavaScript into an HTML page is to use the <script> element. <script> defines the following properties:

    • type: Optional. Represents the content type (also known as the Mimi type) of the Footstep language in which the code is written. Usually this property is Text/javascript, but it is not required.
    • src: Optional. Represents an external file that contains the code to execute.
    • async: Optional. Indicates that the footstep should be downloaded immediately, but does not interfere with other actions on the page (only valid for external footsteps).
    • defer: Optional. Indicates that the footstep can be deferred until the document is fully parsed and realistic before execution (only valid for external steps).
    • charset: Optional. Represents the character set of the code specified through SRC. Most browsers ignore his value, so few people use it.
    • crossrigin: Optional. Controls whether the error message is exposed when the script is acquired across domains.
    • nonce: Optional. You can add encrypted random numbers to styles and scripts. The encrypted random number is a randomly generated number and can only be used once, and is regenerated every time the page is requested. A Web site's content security policy can use random numbers to determine whether a particular script or style should be applied on a Web page.
Label Position

The code contained inside the <script> element is interpreted from top to bottom (both embedded JS code or external file). If you use an external file (including a CSS file

and JS file) is placed in the

The browser window is blank. To avoid this problem, the reference is typically placed behind the page content in the <body> element.

delay (defer) and asynchronous (async) Scripts

  The defer property applies only to external files, and is used to indicate that the script does not affect the construction of the page when it executes. In other words, the script is deferred until the entire page has been parsed and then run. Download deferred execution now. Delay script

In the order in which they appear, they are executed before the Domcontentloaded event, but in reality they are not necessarily executed sequentially, nor do they occur before the domcontentloaded event triggers.

Therefore, it is best to include only one deferred script.

  The Async property is similar to defer and only works with external files and tells the browser to download the file immediately. But unlike defer, it is not guaranteed to follow the order in which they are specified. The purpose of the Async attribute is to not allow the page to wait

Two scripts are downloaded and executed to asynchronously load other content on the page. 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. I'm not sure I'll be in domcontentloaded

The event fires before or after execution.

Embed code with external files

It is recommended to use external files, but not forced.

Advantages of external files:

    • Maintainability
    • cacheable: If you use the same file for all two pages, the file is only downloaded once.
    • Adapting to the future
<noscript> Elements

The contents of the <noscript> element are displayed in the following situations:

    • Browser does not support scripting
    • Browsers support scripts, but scripts are disabled

"Reading" "Elevation 3" ch2--using JavaScript 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.