forked from JakHuang/form-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadScript.js
More file actions
30 lines (27 loc) · 784 Bytes
/
loadScript.js
File metadata and controls
30 lines (27 loc) · 784 Bytes
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
function loadScript(src, callback) {
const cb = callback || (() => {})
const $script = document.createElement('script')
$script.src = src
$script.id = src
document.body.appendChild($script)
const onEnd = 'onload' in $script ? stdOnEnd : ieOnEnd
onEnd($script)
function stdOnEnd(script) {
script.onload = () => {
this.onerror = this.onload = null
cb(null, script)
}
script.onerror = () => {
this.onerror = this.onload = null
cb(new Error(`Failed to load ${src}`), script)
}
}
function ieOnEnd(script) {
script.onreadystatechange = () => {
if (this.readyState !== 'complete' && this.readyState !== 'loaded') return
this.onreadystatechange = null
cb(null, script)
}
}
}
export default loadScript