-
Notifications
You must be signed in to change notification settings - Fork 1
/
loads.js
61 lines (60 loc) · 1.65 KB
/
loads.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* 简单封装异步或同步加载资源
*/
window.loads = window.loads || function(_urls,callMyFun,async){
var _lds = {
js:function(_url,callback){
var _scipt = document.createElement("script");
_scipt.setAttribute("type", "text/javascript");
_scipt.setAttribute("src", _url);
document.getElementsByTagName("head")[0].appendChild(_scipt);
if (navigator.userAgent.indexOf("IE") >= 0) {
_scipt.onreadystatechange = function() {
if (_scipt && (_scipt.readyState == "loaded" || _scipt.readyState == "complete")) {
_scipt.onreadystatechange = null;
callback ? callback() : '';
}
};
} else {
_scipt.onload = function() {
_scipt.onload = null;
console.log('js加载完成')
callback ? callback() : '';
};
}
},
css:function(_url,callback){
var _head = document.getElementsByTagName('head')[0],
_link = document.createElement('link');
_link.setAttribute("type", "text/css");
_link.setAttribute("rel", "stylesheet");
_link.setAttribute("href", _url);
_head.appendChild(_link);
_link.onload = function() {
_head.onload = null;
console.log('css加载完成')
callback ? callback() : '';
};
}
};
var index = 0,asyncNum = 0;
if(_urls && _urls.length > 0){
load(_urls[index]);
}
function load(_url){
_lds[_url.indexOf('css!') == 0 ? "css":"js"]((function(_url){
return _url.replace(/css!|js!/g,'');
})(_url),function(){
if(!async){
++index == _urls.length ? callMyFun():load(_urls[index])
}else{
if(++asyncNum == _urls.length){
callMyFun();
}
}
});
if(async && ++index != _urls.length){
load(_urls[index])
}
}
}