Asynchronous loading JS

Source: Internet
Author: User

There are three ways to asynchronously load JS in the Scripting House:

By default JavaScript is loaded synchronously, that is, JavaScript loading is blocked, after the elements to wait for the JavaScript to be loaded before loading, for some meaning is not very large javascript, if placed in the page header will cause the load is very slow , it can seriously affect the user experience.

(1) defer, only IE support
Definition and usage of the defer property (I am from the W3school website)
The Defer property specifies whether script execution is deferred until the page is loaded.
Some JavaScript scripts document.write methods to create the current document content, and other scripts are not necessarily.

If your script does not change the contents of the document, you can add the defer attribute to the <script> tag to speed up the processing of the document. Because the browser knows it will be able to safely read the rest of the document without executing the script, it will defer the interpretation of the script until the document has been displayed to the user.
Example:

<script type= "Text/javascript" defer= "defer" >alert (document.getElementById ("P1"). Firstchild.nodevalue);

(2) Async:
Definition and usage of async (is the attribute of HTML5)
The async attribute specifies that once the script is available, it executes asynchronously.
Example:

Note: The Async property applies only to external scripts (only when using the SRC attribute).
Note: There are several ways to execute external scripts:
• If async= "Async": The script executes asynchronously relative to the rest of the page (the script will be executed when the page resumes parsing)
• If Async is not used and defer= "defer": The script will execute when the page finishes parsing
• If neither async nor defer is used: reads and executes the script immediately before the browser continues parsing the page

(3) Create script, insert into Dom , callback after loading, see code:

functionloadscript (URL, callback) {varScript = document.createelement_x ("Script") Script.type= "Text/javascript";if(script.readystate) {//IEScript.onreadystatechange =function(){if(Script.readystate = = "Loaded" | |script.readystate= = "complete") {Script.onreadystatechange=NULL; callback ();}};} Else{//Others:firefox, Safari, Chrome, and OperaScript.onload =function() {callback ();};} SCRIPT.SRC=url;document.body.appendchild (script);} 

Asynchronously loading JS

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.