Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
jquery $.getScript() replacement with vanilla JavaScript
"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