Created
February 5, 2021 10:31
-
-
Save kre1z0/e93080c70776a365da2121cd2233e37f to your computer and use it in GitHub Desktop.
function for include script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function script(url) { | |
if (Array.isArray(url)) { | |
let self = this; | |
let prom = []; | |
url.forEach(function (item) { | |
prom.push(self.script(item)); | |
}); | |
return Promise.all(prom); | |
} | |
return new Promise(function (resolve, reject) { | |
let r = false; | |
let t = document.getElementsByTagName("script")[0]; | |
let s = document.createElement("script"); | |
s.type = "text/javascript"; | |
s.src = url; | |
s.async = true; | |
s.onload = s.onreadystatechange = function () { | |
if (!r && (!this.readyState || this.readyState === "complete")) { | |
r = true; | |
resolve(this); | |
} | |
}; | |
s.onerror = s.onabort = reject; | |
t.parentNode.insertBefore(s, t); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment