Skip to content

Instantly share code, notes, and snippets.

@kmonsoor
Created July 3, 2018 14:12
Show Gist options
  • Save kmonsoor/ed54d600d6bfb0fc6b6870115d4f29d5 to your computer and use it in GitHub Desktop.
Save kmonsoor/ed54d600d6bfb0fc6b6870115d4f29d5 to your computer and use it in GitHub Desktop.
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);
};
@ndvbd
Copy link

ndvbd commented Nov 7, 2022

This doesn't set the request header "accept" to be identical to what jquery does

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment