How to dynamically create the script tag onload in IE8 is invalid _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to dynamically create the script tag onload in IE8, which involves adjustments to the javascript loading sequence and has some reference value, for more information about 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:

The 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? 7.1.2 ", function (){
Console. log ("From jQuery ");
});

LoadJs ("test. js? 7.1.2 ", function (){
Console. log ("From test. js? 7.1.2 ");
});
Test. js:
Console. log (typeof jQuery );


Running result:

The code is as follows:

Undefined // test. js runtime, jQuery has not been 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:

The 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:

The 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:

The code is as follows:

LoadJs ("js/jquery-1.11.0.min.js? 7.1.2 ", function (){

Console. log ("From jQuery ");

LoadJs ("test. js? 7.1.2 ", function (){
Console. log ("From test. js? 7.1.2 ");
});
});


Execution result:

The 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:

The 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? 7.1.2 ", function (){
Console. log ("From jQuery ");
});

LoadJs ("test. js? 7.1.2 ", function (){
Console. log ("From test. js? 7.1.2 ");
});


The execution result sequence is also different:

The 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

The code is as follows:

LoadJs ("test. js? 7.1.2 ", function (){
Console. log ("From test. js? 7.1.2 ");
});

LoadJs ("js/jquery-1.11.0.min.js? 7.1.2 ", function (){
Console. log ("From jQuery ");
});


The execution results are different, similar to sequential loading:

The 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.