Non-clogging delay scripting high performance

Source: Internet
Author: User

1. Delay resolution

<script type= "Text/javascript" src= "file1.js" defer></script> defer tells the parser that the script does not modify the DOM and can be resolved later.

Can also be written in the Web page script <script defer>    alert ("defer"); </script> 2, dynamically generated script connection function loadscript (URL, Callback) {    var script = document.createelement ("script"),          Script.type = "Te Xt/javascript ";     if (script.readystate) {                                           ,         &N Bsp            //IE        Script.onreadystatechange = function ()  {&nb Sp           if (script.readystate = = "Loaded" | | script.readystate = = "complete")  {  &NB Sp             Script.onreadystatechange = null;            &NBSP ;   Callback ();           }       };   } else {  & nbsp   &NBSp                          ,         &NB Sp                            //others
Script.onload = function () {
Callback ();
};
}
script.src = URL;
document.getElementsByTagName ("Head") [0].appendchild (script);
How to use: Loadscript ("File1.js", function () {    Loadscript ("File2.js", function () {        & nbsp        //There are three dynamically loaded scripts, note the order of loading, Firefox and opera are executed in the order you wrote them, and some browsers execute in the order they were returned from the server         Loadscript ("File3.js", function () {            alert ("All Files is loaded!");        });   });   3, Ajax script injected var xhr = new XMLHttpRequest () Xhr.open ("Get", "File1.js", true); Xhr.onreadystatechange = function ()  {    if (xhr.readystate = = 4)  {        if (Xhr.status >= & & Xhr.status < 300 | | Xhr.status = = 304)  {            var script = document.createelement ("script");  & nbsp               Script.type = "Text/javascript";                  Script.text = xhr.responsetext;       &nbsp     Document.body.appendChild (script);       }   }};xhr.send (NULL);     recommended mode (second), write a dynamically generated script function on the page, and dynamically load a script that can defer execution <script type= "Text/javascript" >function loadscript (URL, callback)  {    var script = Document.createelement ("script"),          Script.type = "Text/javascript";     if (script.readystate)  {                                                 //ie        Script.onreadystatechange = function ()  {             if (script.readystate = = "L Oaded "| |  script.readystate = = "complete")  {                Script.onreadystate Change = null;                callback ();            }       };   } else {                  &NBS P           &NBSp                          ,         &NB Sp  //others        script.onload = function () {            callback () ;       };   }    SCRIPT.SRC = url;    document.getElementsByTagName (" Head ") [0].appendchild (script);}  loadscript ("The-rest.js", function () {    Application.init ();}); </script>

Non-clogging delay scripting high performance

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.