Skip to content

Instantly share code, notes, and snippets.

@guilsa
Created December 8, 2021 21:43
Show Gist options
  • Save guilsa/c20acdc447843c41c94cb365ff17b138 to your computer and use it in GitHub Desktop.
Save guilsa/c20acdc447843c41c94cb365ff17b138 to your computer and use it in GitHub Desktop.
Load JavaScript files dynamically from browser console
// 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