-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic-loader-amd.js
More file actions
44 lines (39 loc) · 1.13 KB
/
basic-loader-amd.js
File metadata and controls
44 lines (39 loc) · 1.13 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
define(['module'], function (module) {
'use strict';
function _load(tag) {
return function (url) {
// This promise will be used by Promise.all to determine success or failure
return new Promise(function (resolve, reject) {
var element = document.createElement(tag);
var parent = 'body';
var attr = 'src';
// Important success and error for the promise
element.onload = function () {
return resolve(url);
};
element.onerror = function () {
return reject(url);
};
// Need to set different attributes depending on tag type
switch (tag) {
case 'script':
element.async = true;
break;
case 'link':
element.type = 'text/css';
element.rel = 'stylesheet';
attr = 'href';
parent = 'head';
}
// Inject into document to kick off loading
element[attr] = url;
document[parent].appendChild(element);
});
};
}
module.exports = {
css: _load('link'),
js: _load('script'),
img: _load('img')
};
});