JS elevation 2. Using JavaScript in HTML (2)

Source: Internet
Author: User

1. Delay Script defer

Setting the Defer property in the <script> element is equivalent to telling the browser to download it now, but defer execution. The script in <script> is deferred until the browser encounters the

Note:

(1) The HTML5 specification requires scripts to be executed in the order in which they appear, so the first deferred script executes before the second deferred script, which is executed before the domcontentloaded event.

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

(2) HTML5 specifies that the defer property applies only to external script files, so HTML ignores the defer property set for the embedded script.

IE4~IE7 also supports the defer property of the embedded script, but IE8 fully supports the HTML5 specification at the beginning.

(3) Some browsers ignore the defer property and process the script as usual, so putting the deferred script at the bottom of the page is still the best option.

2. Async Script Async

The Async property can asynchronously load the rest of the page by not allowing the page to wait for the script to be downloaded and executed. For this reason, it is recommended that asynchronous scripts do not modify the DOM during loading.

Note:

Async applies only to external script files and tells the browser to download the file immediately. But unlike defer, the scripts that mark the async script are not executed in the order in which they were specified.

Asynchronous scripts must be executed before the Load event of the page, but may be executed before or after the domcontentloaded event is triggered.

3. Usage in XHTML (Extensible Hypertext Markup Language) is much stricter than HTML.

eg

1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4     <MetaCharSet= "UTF-8">5     <title>Test</title>6     <Scripttype= "Text/javascript">7      functionCompare (A, b) {8          if(A<b) {9 Alert ("A is less than B");Ten          } One          Else if(A>b) { A Alert ("A is greater than B"); -          } -          Else{ the Alert ("A is equal to B"); -          } -      } - </Script> + </Head> - <Body> +     <Buttontype= "button"onclick= "Compare (3,10)">Try it</Button> A </Body> at </HTML>

Operation Result:

 

The same code is not valid in XHTML, and the result is as follows:

  Because the less than sign "<" in the comparison statement a < b is interpreted as starting a new label in XHTML. However, as a label, the less-than-sign cannot be followed by a space, resulting in a syntax error.

P15~P17 content Follow-up understanding.

JS elevation 2. Using JavaScript in HTML (2)

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.