-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload.js
More file actions
82 lines (70 loc) · 2.25 KB
/
load.js
File metadata and controls
82 lines (70 loc) · 2.25 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
var $JSAsync = {
modules:{},
module: function (attr) {
var self = this;
// Set Module Defaults
self.data = {
key: null, // Default Attributes for module
active: 1, // Checks whether the item is active
sample: 1, // Percent of the time that it gets run
includes: [], // External script includes
complete: null, // Callback actions after scripts have been loaded
ready: 0, // Do not change these
scripts:0
}
// Add attributes to module
for (var at in self.data) { self.data[at] = attr[at] ? attr[at] : self.data[at]; }
// Load Module based on attributes
self.load = function () {
if (self.data.includes.length) {
var i = 0; while(i < self.data.includes.length) {
var src = self.data.includes[i];
self.data.includes[i] = document.createElement('script');
self.data.includes[i].async = true;
self.data.includes[i].type = 'text/javascript';
self.data.includes[i].src = src;
self.data.includes[i].onload = self.data.includes[i].onreadystatechange = function () {
if (!self.ready && (!self.data.readyState || self.data.readyState == 'complete')) {
self.data.scripts++;
if (self.data.scripts == self.data.includes.length) {
if (typeof(self.data.complete) == "function"){
self.data.complete();
} else {
self.data.complete = new Function(self.data.complete);
self.data.complete();
}
self.ready = true;
}
}
}
document.body.appendChild(self.data.includes[i]);
i++;
}
} else {
if (typeof(self.data.complete) == "function") {
self.data.complete();
} else {
self.data.complete = new Function(self.data.complete);
self.data.complete();
}
self.ready = true;
}
};
// Log the module for debuggin
this.log = function () { window.console.log(this); };
// Initialize Module
self.load();
},
load: function (module) {
var self = this;
if (module instanceof Array) {
var i = 0; while (i < module.length) {
self.modules[module[i].key] = new this.module(module[i]);
i++;
}
} else {
self.modules[module.key] = new this.module(module);
}
},
log: function () { window.console.log(this); }
};