- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.js
More file actions
Latest commit
37 lines (29 loc) · 1.15 KB
/
Copy pathloader.js
File metadata and controls
37 lines (29 loc) · 1.15 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
varloader=function(){};loader.prototype={load:function(e,t){this.loadCount=0;this.totalRequired=e.length;this.callback=t;for(varn=0;n<e.length;n++){this.writeScript(e[n])}},loaded:function(e){this.loadCount++;if(this.loadCount==this.totalRequired&&typeofthis.callback=='function')this.callback.call()},writeScript:function(e){vart=this;varn=document.createElement('script');n.async=true;n.src=e;n.addEventListener('load',function(e){t.loaded(e)},false);varr=document.getElementsByTagName('head')[0];r.appendChild(n)}}
// Basic loading!
newloader().load(['script.js']);
// Mutiple scripts!
newloader().load(['script1.js','script2.js']);
// Chain loading!
newloader().load(['//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'],
function(){
console.log('jQuery loaded');
newloader().load(['script.js'],
function(){
console.log('script.js loaded');
}
);
}
);
// Advanced callbacks!
newloader().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);
},
});
}
);