1, defer, only support IE
The definition and usage of the defer property (I am from the W3school Web site) Defer property specifies whether to defer execution of the script until the page loads. 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); </script>
2. Async:
The definition and usage of async (which is a property of HTML5) The Async attribute specifies that once a script is available, it executes asynchronously.
Example:
<script type= "Text/javascript" src= "Demo_async.js" async= "Async" ></script>
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 executes when the page finishes parsing
If neither async nor defer is used: reads and executes the script immediately before the browser continues to parse the page
3. Create script, insert into DOM, callback code after loading:
Function loadscript (Url, callback) { var script = Document.createelement_x ("script") script.type = "Text/javascript"; if (script.readystate) { //ie script.onreadystatechange = function () { if (script.readystate == "Loaded" | | script.readyState == "complete") { script.onreadystatechange = null; callback (); }}; }else{ //others: firefox, safari, chrome, and opera Script.onload = function () { callback (); }; } script.src = url; document.body.appendchild ( script); }
Asynchronous loading JS