Created
March 13, 2023 15:12
-
-
Save dragon-fish/87bb943980fe9402c585044d7cd39cb4 to your computer and use it in GitHub Desktop.
loadScriptAsync.ts
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 loadScriptAsync(src = '', { isModule = false, noCache = false }) { | |
return new Promise((resolve, reject) => { | |
const script = document.createElement('script') | |
script.async = true | |
if (noCache) { | |
src += `${src.includes('?') ? '&' : '?'}_=${Date.now()}` | |
} | |
script.src = src | |
isModule && (script.type = 'module') | |
script.addEventListener('load', resolve) | |
script.addEventListener('error', reject) | |
document.body.appendChild(script) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment