-
-
Save tacensi/d52cf8ffd3bdd87d13f14793ca43efc1 to your computer and use it in GitHub Desktop.
jquery $.getScript() replacement with vanilla JavaScript
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
"use strict"; | |
// src: https://stackoverflow.com/a/28002292 | |
const getScript = (source, callback) => { | |
var script = document.createElement('script'); | |
var prior = document.getElementsByTagName('script')[0]; | |
script.async = 1; | |
script.onload = script.onreadystatechange = (_, isAbort) => { | |
if (isAbort || !script.readyState || /loaded|complete/.test(script.readyState)) { | |
script.onload = script.onreadystatechange = null; | |
script = undefined; | |
if (!isAbort) if (callback) callback(); | |
} | |
}; | |
script.src = source; | |
prior.parentNode.insertBefore(script, prior); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment