-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (29 loc) · 1.18 KB
/
index.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
var loadScriptCache = {};
function loadScript(url, options) {
var debug = options && options.debug || false;
if (debug) console.log("[easy-script-loader] starting to load", url);
if (!loadScriptCache[url]){
loadScriptCache[url] = new Promise(function (resolve, reject) {
var script = document.createElement('script');
script.src = url;
script.onload = function() {
if (debug) console.log("[easy-script-loader] successfully loaded", url);
resolve();
}
script.onerror = function() {
if (debug) console.error("[easy-script-loader] failed to load", url);
reject();
};
if (document.body) {
document.body.appendChild(script);
} else {
document.head.appendChild(script);
}
});
}
return loadScriptCache[url];
}
if (typeof window !== 'undefined') window.loadScript = loadScript;
if (typeof self !== 'undefined') self.loadScript = loadScript;
if (typeof global !== 'undefined') global.loadScript = loadScript;
if (typeof module !== 'undefined') module.exports = loadScript;