You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
568 B
22 lines
568 B
|
3 years ago
|
const scripts = {}
|
||
|
|
|
||
|
|
export default function loadScript (globalName, src, callback) {
|
||
|
|
const globalObject = typeof globalName === 'string' ? window[globalName] : globalName
|
||
|
|
if (globalObject) {
|
||
|
|
callback()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
let callbacks = scripts[src]
|
||
|
|
if (!callbacks) {
|
||
|
|
callbacks = scripts[src] = []
|
||
|
|
const script = document.createElement('script')
|
||
|
|
script.src = src
|
||
|
|
document.body.appendChild(script)
|
||
|
|
script.onload = function () {
|
||
|
|
callbacks.forEach(callback => callback())
|
||
|
|
delete scripts[src]
|
||
|
|
}
|
||
|
|
}
|
||
|
|
callbacks.push(callback)
|
||
|
|
}
|