-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.js
63 lines (51 loc) · 1.41 KB
/
load.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
62
63
// simple browser asset loader - supports both .js and .css
/**
* load
* @param array<string> paths paths to assets
* @param function cb callback to invoke once all assets have loaded
*/
function load(paths, cb){
var len=paths.length, count=0;
function onScriptLoaded(path){ // if all scripts have been loaded, call cb
count++;
console.log('loaded %s of %s (%s)', count, len, path);
if(count === len && 'function' === typeof cb) cb(count, paths);
}
function addScript(path){
var type=path.toLowerCase().match(/\.js$/) ? 'js' : 'css',
targ=('js' === type)
? document.getElementsByTagName('body')[0]
: document.getElementsByTagName('head')[0],
el=document.createElement(type === 'js' ? 'script' : 'link'),
loaded=false,
onLoad=function(){
if((el.readyState && el.readyState !== "complete" && el.readyState !== "loaded")
|| loaded){
return false;
}
el.onload=
el.onreadystatechange=
null;
loaded=true;
onScriptLoaded(path);
};
el.onload=
el.onreadystatechange=
onLoad;
switch(type){
case 'js':
el.src=path;
el.async=false;
break;
case 'css':
el.rel='stylesheet';
el.href=path;
break;
}
// console.log('inserting "%s":"%s"', type, path);
targ.insertBefore(el, targ.lastChild);
}
for(var i=0; i<len; i++){
addScript(paths[i]);
}
}