Created
June 4, 2018 13:41
-
-
Save Emp3r/776fc798f64ec6aef4e3a7d56c25327b to your computer and use it in GitHub Desktop.
How to dynamically load external javascript code or css styles with JS functions
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 loadScript(src) { | |
return new Promise((resolve, reject) => { | |
const script = document.createElement("script"); | |
script.type = "text/javascript"; | |
script.onload = resolve; | |
script.onerror = reject; | |
script.src = src; | |
document.getElementsByTagName("head")[0].appendChild(script); | |
}); | |
} | |
function loadLink(href) { | |
return new Promise((resolve, reject) => { | |
const link = document.createElement("link"); | |
link.rel = "stylesheet"; | |
link.onload = resolve; | |
link.onerror = reject; | |
link.href = href; | |
document.getElementsByTagName("head")[0].appendChild(link); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment