Comment on the four asynchronous loading methods of js,
Js asynchronous loading methods.
Solution 1:$ (Document). ready
<! DOCTYPE html>
Comments:
1. jquery needs to be referenced
2. compatible with all browsers.
Solution 2:<Script> tag async = "async" attribute
Definition and usage of async (HTML5 attributes)
The async attribute specifies that the script will be executed asynchronously once the script is available.
Example:
<script type="text/javascript" src="xxxxxxx.js" async="async"></script>
Comments:
1. New properties in HTML5 are supported by Chrome, FF, IE9 & IE9 + (IE6 ~ 8 is not supported ). In addition, this method cannot ensure that scripts are executed in sequence.
2. The async attribute is only applicable to external scripts (only when the src attribute is used ).
Solution 3:<Script> tag's defer = "defer" attribute
The defer attribute specifies whether the script execution is delayed until the page is loaded.
Some javascript scripts use the document. write method to create the current document content. Other scripts are not necessarily the same.
If your script does not change the content of the document, you can add the defer attribute to the <script> label to speed up document processing. Because the browser knows that it will be able to safely read the rest of the document without executing the script, it will postpone 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>
Comments:Compatible with all browsers. In addition, this method ensures that all scripts with the defer attribute are executed in order.
Solution 4:Dynamically create <script> tags
Example:
<!DOCTYPE html>
Comments:Compatible with all browsers.
The above is a distinction between the four asynchronous loading methods of js, hoping to help you learn about asynchronous loading of js.
Articles you may be interested in:
- Relatively simple code for loading JS files Asynchronously
- Js achieves Image pre-loading (js operates Image object attribute complete, event onload asynchronously loads images)
- Javascript asynchronous loading (Loading Method of browsers in javascript)
- Implementation principle of synchronous loading and asynchronous loading of javascript files
- Three solutions for js asynchronous loading
- Struts2 + jquery + json Implement Asynchronous Data Loading (self-writing)
- Example of asynchronous loading of javascript loadScript
- JavaScript asynchronous loading analysis