-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
41 lines (34 loc) · 871 Bytes
/
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
32
33
34
35
36
37
38
39
40
41
function load (urls, test, callback) {
let remaining = urls.length
function maybeCallback () {
remaining = --remaining
if (remaining < 1) {
callback()
}
}
if (test()) {
return callback()
}
for (const { type, url, content, options = { async: true, defer: true }} of urls) {
const isScript = type === 'script'
const tag = document.createElement(isScript ? 'script': 'link')
const attribute = isScript ? 'src' : 'href'
const hasUrl = Boolean(url).valueOf()
if (isScript) {
tag.async = options.async
tag.defer = options.defer
} else {
tag.rel = 'stylesheet'
}
if (hasUrl) {
tag[attribute] = url
} else {
tag.appendChild(
document.createTextNode(content)
)
}
tag.onload = maybeCallback
document.body.appendChild(tag)
}
}
export default load