异步加载javascript

//一、模型

function loadScript(url) {
if (url && url.length > 0) {
var script;
for (var i = 0; i < url.length; i++) {
var l_url = url[i];
script = document.createElement("script");
script.type = "text/javascript";
if (script.readyState) {//ie
script.onreadystatechange = function() {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = "null";
if (l_url.callback) { l_url.callback(); }
}
}
}
else {//其他浏览器
script.onload = function() {
if (l_url.callback) { l_url.callback(); }
}
}
script.src = l_url.src;
document.getElementsByTagName("head")[0].appendChild(script);
}
}
}

//二、实战

/*用法:
LoadJS.URL = [{ src: '<%=Page.ResolveUrl("~/Scripts/My97DatePicker/WdatePicker.js") %>' },
{ src: '<%=Page.ResolveUrl("~/Web/srv/srvscript.aspx") %>' },
{ src: '<%=Page.ResolveUrl("~/Scripts/wsdat.wsjsondat?defid=db_dveda.v_data_dict&fmtid=json&strparam={}&dStyle=jsonfile&callback=g_dict") %>' },
{ src: '<%=Page.ResolveUrl("~/Scripts/jquery-1.8.3.js") %>' },
{ src: '<%=Page.ResolveUrl("~/Scripts/jquery.cookie.js") %>' },
{ src: '<%=Page.ResolveUrl("~/Scripts/jquery.easydrag.handler.beta2.js") %>' },
{ src: '<%=Page.ResolveUrl("~/Scripts/powerFloat/js/jquery-powerFloat.js") %>' },
{ src: '<%=Page.ResolveUrl("~/Scripts/jquery-ui-1.9.2.custom.js") %>' },
{ src: '<%=Page.ResolveUrl("~/Scripts/dynatree/jquery.dynatree.js") %>' },
{ src: '<%=Page.ResolveUrl("~/Scripts/ws.data.srv.js") %>' },
{ src: '<%=Page.ResolveUrl("~/Scripts/admin.js") %>' },
{ src: '<%=Page.ResolveUrl("~/Scripts/pagebase.js") %>' },
{ src: '<%=Page.ResolveUrl("~/Scripts/ember/handlebars-1.0.0.beta.6.js") %>' },
{ src: '<%=Page.ResolveUrl("~/Scripts/ember/ember-1.0.0-pre.2.min.js") %>' },
{ src: '<%=Page.ResolveUrl("~/Scripts/ember/ws.ember.base.js") %>' }
];
LoadJS.addjs({ src: 'http://www.cnblogs.com/Scripts/basemanage/articlemanage.js' });
LoadJS.load();
*/
//url[{src:'',callback:function(){}}]
var LoadJS = {
URL: [],
addjs: function(url) {
var me = this;
me.URL.push(url);
console.info(me.URL);
},
load: function() {
var me = this;
var url = me.URL;
if (url && url.length > 0) {
for (var i = 0; i < url.length; i++) {
if ((i + 1) < url.length) {
url[i].next_url = url[i + 1];
}
}
me.loadScript(url[0]);
}
},
//{src:'',callback:function(){},next_url:{}}
loadScript: function(url) {
var me = this;
var l_url = url;
var script = document.createElement("script");
script.type = "text/javascript";
if (script.readyState) {//ie
script.onreadystatechange = function() {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = "null";
if (l_url.callback) { l_url.callback(); }
if (l_url.next_url) { me.loadScript(l_url.next_url); }
}
}
}
else {//其他浏览器
script.onload = function() {
if (l_url.callback) { l_url.callback(); }
if (l_url.next_url) { me.loadScript(l_url.next_url); }
}
}
script.src = l_url.src;
document.getElementsByTagName("head")[0].appendChild(script);
}

}

posted @ 2013-05-15 13:22  kuailewangzi1212  阅读(231)  评论(0编辑  收藏  举报