1
0
mirror of https://github.com/PanJiaChen/vue-element-admin.git synced 2025-08-10 12:01:57 +08:00
vue-element-admin/docs/.vuepress/theme/dynamic-load-script.js
Yamel Senih f45a57178a
Feature/#doc add documentation (#798)
* Add support to x vversion from npm

* Add support to x vversion from npm

* Add support to x vversion from npm

* Add documentation for current repository
2021-04-29 12:23:48 -04:00

41 lines
1.2 KiB
JavaScript

const dynamicLoadScript = (src, callback, id) => {
const existingScript = document.getElementById(src)
const cb = callback || function() {}
if (!existingScript) {
const script = document.createElement('script')
script.src = src // src url for the third-party library being loaded.
script.id = id || src
script.async = true
document.body.appendChild(script)
const onEnd = 'onload' in script ? stdOnEnd : ieOnEnd
onEnd(script, cb)
}
if (existingScript && cb) cb(null, existingScript)
function stdOnEnd(script, cb) {
script.onload = function() {
// this.onload = null here is necessary
// because even IE9 works not like others
this.onerror = this.onload = null
cb(null, script)
}
script.onerror = function() {
this.onerror = this.onload = null
cb(new Error('Failed to load ' + src), script)
}
}
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