How to dynamically add js files in ie

Source: Internet
Author: User

Here we will only discuss browsing that supports parallel downloads. There are roughly two types: one is to execute in the order of adding to the DOM tree, and the other is to execute in the order of download completion; in this way, if JavaScript files are dependent on each other and are executed in the download order, an error will be reported if no cache is available (normally, an error will be reported for the first execution, http return status 200. If the cache is not disabled and the http status is 304, no error will be reported)
Ie executes js code in the order of completion of http download. First, let's look at the following code: Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml" xml: lang = "zh-CN" lang = "zh-CN">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> Model </title>
<Meta http-equiv = "Content-language" content = "zh-CN"/>
</Head>
<Body>
<Div id = "page">
</Div> <! -- Page end! -->
<Script type = "text/javascript">
// <! [CDATA [
Var js = document. createElement ('script ');
Js. type = 'text/javascript ';
Js. src = 'alert. js ';
If (js. readyState ){
Js. onreadystatechange = function (){
If (js. readyState = "loaded" | js. readyState = "complete "){
Alert (js. readyState );
Document. getElementsByTagName ('head') [0]. appendChild (js );
}
};
} Else {
Document. getElementsByTagName ('head') [0]. appendChild (js );
Js. onload = function (){
Alert ('loaded not in ie! ');
};
}
//]>
</Script>
</Body>
</Html>

The content of the dynamically loaded alert. js file is: alert ('in alert. js ');
After testing (ie8), we can find that the pop-up content is: loaded, in alert. js, complete
For more information, see the onreadystatechange event when I add a script to the DOM under ie (other browsers have the onload event), while js in the event. the status of readyState changes to: loading, loaded, and complete)
From the code, we can see that I added the scrip node to the DOM in the event ......
Therefore, we can conclude that http download starts when ie creates a scrip node and assigns a value to src, this is completely different from other browsers (other browsers only need to add the script node to the DOM for http download), and the code is executed only after the scrip node is added to the DOM tree.
With these conclusions, we can solve the problem of parallel download sequence execution in ie. There are two solutions: one is simultaneous download and sequential execution, the other is to complete the download and then execute it in sequence.
The two methods have their own advantages. Here we provide the code for the latter case (loader. js ):Copy codeThe Code is as follows :/*
* Author: JaiHo
*/
(Function (window ){
Var DOMLoader = (function (){
Var DOMLoader = function (){
Return new DOMLoader. prototype. init ();
};
DOMLoader. prototype = {
JsList: [], js_all: 0, loaded_js: 0,
Head: document. getElementsByTagName ('head') [0],
Init: function (){},
Create_node: function (src ){
Var js = document. createElement ('script ');
Js. type = 'text/javascript ';
This. bindWait (js );
This. jsList [this. jsList. length] = js;
Js. src = src;
},
LoadJS: function (list ){
Len = list. length;
For (var I = 0; I <len; I ++ ){
If (I = len-1)
This. js_all = len;
This. create_node (list [I]);
}
Return this;
},
BindWait: function (js ){
If (arguments. callee. caller! = This. create_node) return;
Var that = this;
If (js. readyState ){
Js. onreadystatechange = function (){
If (js. readyState = 'loaded '){
That. loaded_js ++;
If (that. js_all = that. loaded_js ){
That. head. appendChild (that. jsList. shift ());
}
}
If (js. readyState = "complete "){
Js. onreadystatechange = null;
If (that. jsList. length ){
That. head. appendChild (that. jsList. shift ());
}
}
};
} Else {
Js. onload = function (){
Alert ('not in ie! ');
};
}
Return this;
}
};
DOMLoader. prototype. init. prototype = DOMLoader. prototype;
Return window. DOMLoader = DOMLoader;
})();
}) (Window );

The test example is as follows:Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml" xml: lang = "zh-CN" lang = "zh-CN">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> Loader </title>
<Meta http-equiv = "Content-language" content = "zh-CN"/>
<Style type = "text/css" media = "all">
</Style>
</Head>
<Body>
<Div>
</Div>
<Script type = "text/javascript" src = "loader. js"> </script>
<Script type = "text/javascript">
// <! [CDATA [
Window. onload = function (){
Var loader = DOMLoader ();
Loader. loadJS (['json. js', 'jquery-1.5.min.js ', 'test. js']);
};
//]>
</Script>
</Body>
</Html>

We can see that the three loaded js files are downloaded in parallel.
There is no perfect solution for the parallel download and sequential execution of js files Dynamically Loaded by other browsers. (if so, please advise ..), From this perspective, I personally think that the onreadystatechange event solution for ie is relatively better.

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.