At this time the best way is to introduce, dynamically introduce component JS and style, file load completion call callback, run JS. The code is still very simple.
1. Judge the file load complete. Load state ie for onreadystatechange, others for onload, onerror
Copy Code code as follows:
if (Isie) {
Res.onreadystatechange = function () {
if (res.readystate = = ' Complete ' | | Res.readystate = = ' loaded ') {
Res.onreadystatechange = null;
Callback ();
_self.loadedui[modelname] = true;
}
}
}else{
Res.onload = function () {
Res.onload = null;
Callback ();
_self.loadedui[modelname] = true;
}
Res.onerror = function () {
throw new error (' Res error: ' + modelname+ '. js ');
}
}
2. The naming of all components is best kept consistent, and callback calls are also more convenient. You can also add parameters as needed: Requires, depending on those files; Style,true | | False, whether to load styles, and so on.
3. The removal operation can also have, remove script, style label, delete component
Copy Code code as follows:
(function (window,undefined) {
if (!window.ui) {
Window.ui = {};
}
Dynamically loading the UI's JS
Window.bus = {
Config: {
Version:window.config.version,
CssPath:window.config.resServer + '/css/v3/ui ',
JsPath:window.config.resServer + '/js/v2/ui '
},
Loadedui: {},
Readystatechange:function () {
var ua = Navigator.userAgent.toLowerCase ();
Return Ua.indexof (' msie ') >= 0;
},
Loadres:function (Modelname,prames,callback) {
var _self = this;
var Res = document.createelement (prames.tagname);
For (var k in prames) {
if (k!= ' TagName ') {
Res.setattribute (k,prames[k],0);
}
}
document.getElementsByTagName (' head ') [0].appendchild (Res);
if (This.readystatechange ()) {
Res.onreadystatechange = function () {
if (res.readystate = = ' Complete ' | | Res.readystate = = ' loaded ') {
Res.onreadystatechange = null;
Callback ();
_self.loadedui[modelname] = true;
}
}
}else{
Res.onload = function () {
Res.onload = null;
Callback ();
_self.loadedui[modelname] = true;
}
Res.onerror = function () {
throw new error (' Res error: ' + modelname+ '. js ');
}
}
},
Removeui:function (modelname) {
if (!modelname) {
return True
};
This.loadedui[modelname] = false;
var head = document.getElementsByTagName (' head ') [0];
var model_js = document.getElementById (' j_model_ ' +modelname + ' _js ');
var model_css = document.getElementById (' j_model_ ' +modelname + ' _css ');
if (Model_js && model_css) {
Delete Window.ui[modelname];
Head.removechild (MODEL_JS);
Head.removechild (MODEL_CSS);
return true;
}else{
return false;
}
},
Loadui:function (modelname,callback,setting) {
if (!modelname) {
return True
};
Callback = Callback | | function () {};
if (this.loadedui[modelname] = = True) {
Callback ();
return True
}
var deafult_setting = {
Style:true,
Js:true,
Requires: []
}
for (var key in setting) {
Deafult_setting[key] = Setting[key];
}
deafult_setting[' style '] = = = True && this.loadres (modelname,{
ID: ' j_model_ ' +modelname + ' _css ',
Name:modelname,
TagName: ' Link ',
Type: ' Text/css ',
Rel: ' stylesheet ',
Href:this.config.cssPath + '/' + modelname + '. css?v= ' + this.config.version
});
deafult_setting[' js '] = = = True && this.loadres (modelname,{
ID: ' j_model_ ' +modelname + ' _js ',
Name:modelname,
TagName: ' Script ',
Type: ' Text/javascript ',
Src:this.config.jsPath + '/' + modelname + '. js?v= ' + this.config.version
},callback);
if (Deafult_setting.requires.length > 0) {
for (var I=0,len = deafult_setting.requires.length;i<len;i++) {
This.loadui (Deafult_setting.requires[i]);
}
}
}
}
}) (window)
How to use
Copy Code code as follows:
Load comment for Feed
Window.bus.loadUi (' New_comment_feed ', function () {
Window.ui.new_comment_feed ($ ("#J_newsList"));
},{
Style:false,
requires:[' emoticon ', ' addfriend ']
});
Load new YY UI
Window.bus.loadUi (' yy ', function () {
Window.ui.yy (options);
},{
Style:false,
requires:[' emoticon ']
});
Load Photolightbox
Window.bus.loadUi (' Photolightbox ', function () {
Window.ui.photoLightbox (Options.urlajaxgetfriendphoto, options.urlcommentotherphoto,$ ("#J_newsList"), Options.myuid,options.myname);
});