-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.js
37 lines (29 loc) · 1.15 KB
/
loader.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
var loader=function(){};loader.prototype={load:function(e,t){this.loadCount=0;this.totalRequired=e.length;this.callback=t;for(var n=0;n<e.length;n++){this.writeScript(e[n])}},loaded:function(e){this.loadCount++;if(this.loadCount==this.totalRequired&&typeof this.callback=='function')this.callback.call()},writeScript:function(e){var t=this;var n=document.createElement('script');n.async=true;n.src=e;n.addEventListener('load',function(e){t.loaded(e)},false);var r=document.getElementsByTagName('head')[0];r.appendChild(n)}}
// Basic loading!
new loader().load(['script.js']);
// Mutiple scripts!
new loader().load(['script1.js','script2.js']);
// Chain loading!
new loader().load(['//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'],
function() {
console.log('jQuery loaded');
new loader().load(['script.js'],
function() {
console.log('script.js loaded');
}
);
}
);
// Advanced callbacks!
new loader().load(['//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js'],
function() {
WebFont.load({
google: {
families: ['Droid Sans Mono']
},
fontactive: function(fontFamily) {
console.log('Loaded ' + fontFamily);
},
});
}
);