Skip to content

Instantly share code, notes, and snippets.

@dragon-fish
Created September 24, 2025 09:25
Show Gist options
  • Select an option

  • Save dragon-fish/9620734d6a77b80432b2cb103508dec8 to your computer and use it in GitHub Desktop.

Select an option

Save dragon-fish/9620734d6a77b80432b2cb103508dec8 to your computer and use it in GitHub Desktop.
/**
* @param {string} src
* @param {Record<string, string>?} attrs
* @returns {Promise<HTMLScriptElement>}
*/
function loadScript(src, attrs = {}) {
return new Promise((res, rej) => {
const s = Object.assign(document.createElement('script'), { src })
Object.entries(attrs).forEach(([k, v]) => k && v !== void 0 && s.setAttribute(k, v))
s.onload = () => res(s)
s.onerror = (e) => rej(e)
document.head.appendChild(s)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment