forked from brigand/react-zeroclipboard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
loadScript.js
38 lines (32 loc) · 1.04 KB
/
loadScript.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
var loading = {};
module.exports = function loadScript(src, callback){
if (typeof(window) === 'undefined') return;
// we don't want duplicate script elements
// so we use an array of callbacks instead of
// multiple onload handlers
if (loading[src]) {
loading[src].push(callback);
return;
}
// create the array of callbacks
loading[src] = [callback];
// create a script, and handle success/failure in node callback style
var script = document.createElement('script');
script.onload = function(){
loading[src].forEach(function(cb){
cb();
});
delete loading[src];
};
script.onerror = function(error){
loading[src].forEach(function(cb){
cb(error)
});
delete loading[src];
};
// set the src and append it to the head
// I believe async is true by default, but there's no harm in setting it
script.async = true;
script.src = src;
document.getElementsByTagName("head")[0].appendChild(script);
};