The solution to dynamically creating the script tag onload in IE8 is invalid. ie8onload
This article describes how to dynamically create the script tag onload in IE8. Share it with you for your reference. The specific analysis is as follows:
During the project today, I found a strange problem. The dynamically created script tag cannot trigger the onload event under IE8.
The Code is as follows:
Copy codeThe Code is as follows: var loadJs = function (src, fun ){
Var script = null;
Script = document. createElement ("script ");
Script. type = "text/javascript ";
Script. src = src;
If (typeof fun = "function "){
Script. onload = fun;
}
Document. getElementsByTagName ("head") [0]. appendChild (script );
};
LoadJs ("js/jquery-1.11.0.min.js", function (){
Console. log ("From jQuery ");
});
LoadJs ("test. js", function (){
Console. log ("From test. js ");
});
Test. js:
Console. log (typeof jQuery );
Running result:
Copy codeThe Code is as follows: when undefined // test. js is run, jQuery has not loaded
> Typeof jQuery // runs on the console, but finds the jQuery object to prove the Loading Order.
"Function"
In addition, the script. onload in the above Code is not executed. It is clear that the code has been loaded. Why is it still that the onload is not executed? I checked on the Internet and found that many front-end developers encountered this difficult problem. So I found some alternative solutions, as shown below:
Copy codeThe Code is as follows: var loadJs = function (src, fun ){
Var script = null;
Script = document. createElement ("script ");
Script. type = "text/javascript ";
Script. src = src;
If (typeof fun = "function "){
Script. onreadystatechange = function (){
Var r = script. readyState;
Console. log (src + ":" + r );
If (r = 'loaded' | r = 'complete '){
Script. onreadystatechange = null;
Fun ();
}
};
}
Document. getElementsByTagName ("head") [0]. appendChild (script );
};
Execution result:
Copy codeThe Code is as follows: undefined
Js/jquery-1.11.0.min.js: loading
Test. js: complete
From test. js
Js/jquery-1.11.0.min.js: loaded
From jQuery
The execution step is as follows: the onload function is found, but there is a problem. It is not loaded in order. When the jQuery file loading, test. javascript has already been complete, and the first line executes test. js content. Because test. js is executed before jQuery, undefined is output. So we can rewrite it like this to linearly load it:
Copy codeThe Code is as follows: loadJs ("js/jquery-1.11.0.min.js", function (){
Console. log ("From jQuery ");
LoadJs ("test. js", function (){
Console. log ("From test. js ");
});
});
Execution result:
Copy codeThe Code is as follows: js/jquery-1.11.0.min.js: loading
Js/jquery-1.11.0.min.js: loaded
From jQuery
Function
Test. js: complete
From test. js
This time, the order of execution is completely in the order of our reservation, but the above Code looks awkward and requires layers of nesting, so we found this way:
Copy codeThe Code is as follows: var loadJs = function (src, fun ){
Var script = null;
Script = document. createElement ("script ");
Script. type = "text/javascript ";
Script. src = src;
If (typeof fun = "function "){
Script. onreadystatechange = function (){
Var r = script. readyState;
Console. log (src + ":" + r );
If (r = 'loaded' | r = 'complete '){
Script. onreadystatechange = null;
Fun ();
}
};
}
Document. write (script. outerHTML );
// Document. getElementsByTagName ("head") [0]. appendChild (script );
};
LoadJs ("js/jquery-1.11.0.min.js", function (){
Console. log ("From jQuery ");
});
LoadJs ("test. js", function (){
Console. log ("From test. js ");
});
The execution result sequence is also different:
Copy codeThe Code is as follows: function
Js/jquery-1.11.0.min.js: loaded
From jQuery
Test. js: loaded
From test. js
If you change the loading sequence
Copy codeThe Code is as follows: loadJs ("test. js", function (){
Console. log ("From test. js ");
});
LoadJs ("js/jquery-1.11.0.min.js", function (){
Console. log ("From jQuery ");
});
The execution results are different, similar to sequential loading:
Copy codeThe Code is as follows: undefined
Test. js: loaded
From test. js
Js/jquery-1.11.0.min.js: loaded
From jQuery
I hope this article will help you design javascript programs.