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;   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