In the demand for "loading on demand", we often judge how to judge the completion of Script Loading by returning a callback function when the script is loaded? 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 specific implementation code is as follows:
The Code is as follows:
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;
}
// Execution function
Include_js ('HTTP: // img.jb51.net/jslib/jquery/jquery-1.3.2.min.js );