Load and execute-high performance JavaScript

Source: Internet
Author: User

1. Causes:

  Each time you encounter a <script> tag, the page must stop waiting for the code to download and finish, before proceeding with the rest of the process.

2. Reduce the impact of JavaScript on performance

1. Put all JavaScript files before </body>, which ensures that the page has been rendered before the script executes.

2. Merge scripts. The fewer <script> tags on the page, the faster the load and the quicker the response. Whether the outer chain script or the inline script.

3. There are a variety of non-blocking download JavaScript methods:

1. Using the Defer property of <script>, Async property (IE)

2. Use dynamically created <script> elements to download and execute code

    

function Loadscript (URL, callback) {varScript = Document.createelement ('Script'); Script.type="Text/javascript"; if(script.readystate) {//IE9 belowScript.onreadystatechange =function () {if(script.readystate==' Complete'|| script.readystate=='Loaded') {callback (); }        }    }Else{script.onload=function () {//Firefox chrome Safair operacallback (); }} script.src=URL; Document.head.appendChild (script);}

3. Download the JavaScript code using the XHR object and inject it into the page

  

Load and execute-high performance JavaScript

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.