support ie
This commit is contained in:
parent
92ca2ff0a3
commit
24f082156b
|
@ -8,15 +8,32 @@ const dynamicLoadScript = (src, callback) => {
|
||||||
script.id = src
|
script.id = src
|
||||||
document.body.appendChild(script)
|
document.body.appendChild(script)
|
||||||
|
|
||||||
script.onload = () => {
|
const onend = 'onload' in script ? stdOnEnd : ieOnEnd
|
||||||
if (cb) cb(null, script)
|
onend(script, cb)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (existingScript && cb) cb()
|
||||||
|
|
||||||
|
function stdOnEnd(script, cb) {
|
||||||
|
script.onload = function() {
|
||||||
|
this.onerror = this.onload = null
|
||||||
|
cb(null, script)
|
||||||
}
|
}
|
||||||
script.onerror = () => {
|
script.onerror = function() {
|
||||||
|
// this.onload = null here is necessary
|
||||||
|
// because even IE9 works not like others
|
||||||
|
this.onerror = this.onload = null
|
||||||
cb(new Error('Failed to load ' + src), script)
|
cb(new Error('Failed to load ' + src), script)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (existingScript && cb) cb()
|
function ieOnEnd(script, cb) {
|
||||||
|
script.onreadystatechange = function() {
|
||||||
|
if (this.readyState !== 'complete' && this.readyState !== 'loaded') return
|
||||||
|
this.onreadystatechange = null
|
||||||
|
cb(null, script) // there is no way to catch loading errors in IE8
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default dynamicLoadScript
|
export default dynamicLoadScript
|
||||||
|
|
Loading…
Reference in New Issue