-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetJS.js
More file actions
84 lines (71 loc) · 2.9 KB
/
Copy pathgetJS.js
File metadata and controls
84 lines (71 loc) · 2.9 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(function(win){
var doc = win.document;
var css = function(href, callback) {
var link = doc.createElement("link");
link.rel = "stylesheet";
link.href = href;
link.onload = function(e){
for(i in doc.styleSheets)
if(d = doc.styleSheets[i].href == e.target.href)
return typeof callback === 'function' ? callback() : null;
return setTimeout(e.target.onload);
};
link.onerror = function(e){
console.error('Error '+e.target.tagName+':'+e.target.href+' failed loading.');
return typeof callback === 'function' ? callback() : null;
}
doc.head.appendChild(link);
return link;
};
var js = function(src, callback) {
var script = doc.createElement("script");
script.src = src;
script.async = true;
script.onload = typeof callback == 'function' ? callback : null;
script.onerror = function(e){
console.error('Error '+e.target.tagName+':'+e.target.src+' failed loading.');
return typeof callback === 'function' ? callback() : null;
};
doc.head.appendChild(script);
return script;
};
var getjs = function(urls, callback, loaded){
if(!loaded){
extraLoad.list.push(urls instanceof Array ? urls : [urls]);
extraLoad.callback.push(callback);
if(!extraLoad.loading){
getjs(0, 0, 1);
}
}else{
if(extraLoad.list[0] instanceof Array && extraLoad.list[0].length){
extraLoad.loading = true;
var path = extraLoad.list[0].shift(),
isJS = /\.js$/.test(path),
url = /^https?:/.test(path) ? path : win.location.origin + path,
tag = isJS ? {name:'script',attr:'src',url:url,ext:'js'} : {name:'link',attr:'href',url:url,ext:'css'},
exist = !!doc.querySelector(tag.name+'['+tag.attr+'="'+tag.url+'"],'+tag.name+'['+tag.attr+'="'+path+'"]');
if(!exist){
extraLoad.get[tag.ext](tag.url, function(){
getjs(0, 0, 1);
});
}else{
getjs(0, 0, 1);
}
}else if(typeof extraLoad.callback[0] == 'function'){
extraLoad.callback[0]();
extraLoad.callback[0] = undefined;
getjs(0, 0, 1);
}else{
if(!extraLoad.list.length){
extraLoad.loading = false;
}else{
extraLoad.list.shift();
extraLoad.callback.shift();
getjs(0, 0, 1);
}
}
}
};
win.getjs = win.getJS = getjs;
win.extraLoad = {list:[],callback:[],get:{css:css,js:js},loading:false};
}(window));