JavaScript value delay script and asynchronous script

Source: Internet
Author: User

Html 4.0 defines the defer attribute for the <script> tag, which is used when the table name script executes without affecting the construction of the Page. That is, the script is deferred until the entire page has been parsed, so setting the Defer property in the <script> element is equivalent to telling the browser to download it immediately, but defer execution. But there is a special case, see the following code:

<HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head>    <Metahttp-equiv= "content-type"content= "text/html; charset=utf-8"/>    <title></title>    <Scriptsrc= "aaa.js"defer= "defer"></Script>    <Scriptsrc= "bbb.js"defer= "defer"></Script></Head><Body><DivID= "div"></Div></Body></HTML>

The code above has two external scripts, and they are all set to lazy loading, and the HTML5 specification requires that the script be executed in the order in which they appear, so the first delay script executes before the second delay script, but this is not the case, the deferred script does not necessarily follow the order. So it's best to include only one delay script!

Asynchronous script:

Like defer, async works only with external scripts and tells the browser to download files immediately, and scripts marked as Async are not guaranteed to be executed in the order in which they are specified. The following code:

<HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head>    <Metahttp-equiv= "content-type"content= "text/html; charset=utf-8"/>    <title></title>    <Scriptsrc= "aaa.js"Async></Script>    <Scriptsrc= "bbb.js"Async></Script></Head><Body><DivID= "div"></Div></Body></HTML>

The two external script files in the above code are set to be loaded asynchronously, but the second one may be executed before the first script File. therefore, It is important to make sure that the two are not used.

JavaScript value delay script and asynchronous script

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.