This article mainly introduces how to judge the completion of js Script Loading. If you need it, you can refer to it and hope to help you in the demand for "loading on demand, we often judge that a callback function is returned when the script is loaded. How can we determine whether the script is loaded successfully?
We can use onload to determine the loaded JS object (js. onload), this method is supported by Firefox2, Firefox3, Safari3.1 +, and Opera9.6 + browsers, but not IE6 or IE7. Curve saving-IE6 and IE7 we can use js. onreadystatechange is used to track the status changes (generally loading, loaded, interactive, and complete). When the returned status is loaded or complete, the loading is complete and the callback function is returned.
You need to provide additional instructions on readyState status:
1. In the interactive status, users can participate in the interaction.
2. Opera also supports js. onreadystatechange, but its status is very different from that of IE.
The Code is as follows:
Script
Function include_js (file ){
Var _ doc = document. getElementsByTagName ('head') [0];
Var js = document. createElement ('script ');
Js. setAttribute ('type', 'text/javascript ');
Js. setAttribute ('src', file );
_ Doc. appendChild (js );
If (! /* @ Cc_on! @ */0) {// if not IE
// Firefox2, Firefox3, Safari3.1 +, Opera9.6 + support js. onload
Js. onload = function (){
Alert ('firefox2, Firefox3, Safari3.1 +, Opera9.6 + support js. onload ');
}
} Else {
// IE6 and IE7 support js. onreadystatechange
Js. onreadystatechange = function (){
If (js. readyState = 'loaded' | js. readyState = 'complete '){
Alert ('ie6, IE7 support js. onreadystatechang ');
}
}
}
Return false;
}
Include_js ('HTTP: // www.planabc.net/wp-includes/js/jquery/jquery.js ');
Script