By default, javascript is loaded synchronously, that is, javascript is blocked during loading. The subsequent elements have to wait for javascript to be loaded before they can be loaded again. For some JavaScript that is not very meaningful, if placed in the header, it will cause it to be very slow, which will seriously affect the user experience.
(1) defer, only supports IE
Definition and usage of defer attribute (I excerpted from w3school website)
The defer property specifies whether script execution is delayed until the page loads.
Some javascript script methods are used to create the current document content, but other scripts may not be the case.
If your script does not change the content of the document, add the defer attribute to the <script> tag to speed up processing of the document. Because the browser knows that it will be able to safely read the rest of the document without executing the script, it will delay the interpretation of the script until the document has been displayed to the user.
Example:
<script type="text/javascript" defer="defer">
alert(("p1").);
</script>
(2) async:
Definition and usage of async (it is an attribute of HTML5)
The async attribute specifies that once the script is available, it will be executed asynchronously.
Example:
<script type="text/javascript" src="demo_async.js" async="async"></script>
Note: The async attribute is only applicable to external scripts (only when using the src attribute).
Note: There are multiple ways to execute external scripts:
•If async="async": the script is executed asynchronously relative to the rest of the page (the script will be executed when the page continues to parse)
•If async is not used and defer="defer": the script will be executed when the page completes parsing
•If neither async nor defer: Read and execute the script immediately before the browser continues to parse the page
(3) Create script and insert it into the DOM, callBack after loading, see the code:
function loadScript(url, callback){
var script = document.createElement_x("script")
= "text/javascript";
if (){ //IE
= function(){
if ( == "loaded" ||
== "complete"){
= null;
callback();
}
};
} else { //Others: Firefox, Safari, Chrome, and Opera
= function(){
callback();
};
}
= url;
(script);
}
(1) defer, only supports IE
Definition and usage of defer attribute (I excerpted from w3school website)
The defer property specifies whether script execution is delayed until the page loads.
Some javascript script methods are used to create the current document content, but other scripts may not be the case.
If your script does not change the content of the document, add the defer attribute to the <script> tag to speed up processing of the document. Because the browser knows that it will be able to safely read the rest of the document without executing the script, it will delay the interpretation of the script until the document has been displayed to the user.
Example:
Copy the codeThe code is as follows:
<script type="text/javascript" defer="defer">
alert(("p1").);
</script>
(2) async:
Definition and usage of async (it is an attribute of HTML5)
The async attribute specifies that once the script is available, it will be executed asynchronously.
Example:
Copy the codeThe code is as follows:
<script type="text/javascript" src="demo_async.js" async="async"></script>
Note: The async attribute is only applicable to external scripts (only when using the src attribute).
Note: There are multiple ways to execute external scripts:
•If async="async": the script is executed asynchronously relative to the rest of the page (the script will be executed when the page continues to parse)
•If async is not used and defer="defer": the script will be executed when the page completes parsing
•If neither async nor defer: Read and execute the script immediately before the browser continues to parse the page
(3) Create script and insert it into the DOM, callBack after loading, see the code:
Copy the codeThe code is as follows:
function loadScript(url, callback){
var script = document.createElement_x("script")
= "text/javascript";
if (){ //IE
= function(){
if ( == "loaded" ||
== "complete"){
= null;
callback();
}
};
} else { //Others: Firefox, Safari, Chrome, and Opera
= function(){
callback();
};
}
= url;
(script);
}