100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > javascript开发随笔二 动态加载js和文件【javascript】

javascript开发随笔二 动态加载js和文件【javascript】

时间:2019-10-06 11:26:32

相关推荐

javascript开发随笔二 动态加载js和文件【javascript】

web前端|js教程

动态加载

web前端-js教程

这时候最好的做法就是按需引入,动态引入组件js和样式,文件load完成后调用callback,运行js。代码还是很简便的

1. 判断文件load完成。加载状态ie为onreadystatechange,其他为onload、onerror

神仙道 全套源码,Ubuntu校正电池,tomcat如何解析请求,golang chan爬虫,java 模拟php函数,seo进阶版seo教程lzw

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( es error: + modelName+.js);

}

}

2. 所有组件的命名最好保持一致,callback调用也比较方便。还可以根据需要增加参数比如: requires,依赖于那些文件;style,true || false,是否加载样式,等等。

3. 移除操作也可以有,移除script、style标签、delete组件

php源码案例,vscode未找到编译程序,ubuntu 搜狗 删除,tomcat部署阿里云,爬虫模块sp,java执行php代码,谷歌seo排名怎么样运营,代购网站建设lzw

(function(window,undefined){

if(!window.ui) {

window.ui = {};

}

//动态加载ui的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 != agName){

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( es 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 : ext/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 : ext/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)

使用方法

二手车 源码php,ubuntu首次需要密码,面里白色爬虫,php heatmap,seo333999lzw

// load comment for feed

window.bus.loadUi( ew_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);

});

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。