Created
March 6, 2020 12:52
-
-
Save dsheiko/bd8da5e2460ee11e0521732fe63f8557 to your computer and use it in GitHub Desktop.
Load/execute JavaScript asynchronously
This file contains 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
function loadJs(url){ | |
return new Promise( (resolve, reject) => { | |
if (document.querySelector(`head > script[src="${src}"]`) !== null) return resolve() | |
const script = document.createElement("script") | |
script.src = url | |
script.onload = resolve | |
script.onerror = reject | |
document.head.appendChild(script) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: