Created
December 8, 2021 21:43
-
-
Save guilsa/c20acdc447843c41c94cb365ff17b138 to your computer and use it in GitHub Desktop.
Load JavaScript files dynamically from browser console
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
// source: https://aaronsmith.online/easily-load-an-external-script-using-javascript/ | |
/** | |
* Loads a JavaScript file and returns a Promise for when it is loaded | |
*/ | |
const 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.head.append(script) | |
}) | |
} | |
loadScript('https://code.jquery.com/jquery-3.4.1.min.js') | |
.then(() => loadScript('https://code.jquery.com/ui/1.12.1/jquery-ui.min.js')) | |
.then(() => { | |
// now safe to use jQuery and jQuery UI, which depends on jQuery | |
}) | |
.catch(() => console.error('Something went wrong.')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment