Skip to content

Instantly share code, notes, and snippets.

@ZacharyCouchman
Last active November 22, 2023 10:27
Show Gist options
  • Save ZacharyCouchman/d5df5f6e73a803471386f853de7960c2 to your computer and use it in GitHub Desktop.
Save ZacharyCouchman/d5df5f6e73a803471386f853de7960c2 to your computer and use it in GitHub Desktop.
// dynamically load a script and wait for load event
async function loadScript(url) {
return new Promise((resolve, reject) => {
let ourScript = document.createElement('script');
ourScript.addEventListener('load', () => {
console.log('script was loaded');
resolve('awesome');
});
ourScript.addEventListener('error', () => {
console.log('something went wrong loading ourScript');
reject('sad');
});
// add your script's src here
ourScript.src = url;
document.head.appendChild(ourScript);
});
}
loadScript('https://cdn.jsdelivr.net/npm/...')
.then((result) => console.log(result))
.catch((error) => console.error(error));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment