Skip to content

Instantly share code, notes, and snippets.

@kevinchisholm
Last active June 23, 2021 22:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinchisholm/e0f01f4c18b13de06c0d2922e9445a0a to your computer and use it in GitHub Desktop.
Save kevinchisholm/e0f01f4c18b13de06c0d2922e9445a0a to your computer and use it in GitHub Desktop.
jQuery.getScript Alternatives - Example 4
function getScript(scriptUrl, callback) {
const script = document.createElement('script');
script.src = scriptUrl;
script.onload = callback;
document.body.appendChild(script);
}
// anonymous function as 2nd argument
getScript('https://bit.ly/get-script-example', function () {
console.log('The script load is done.');
});
// Hello, I am a remote script that successfully loaded.
// The script load is done.
// ["foo", "bar", "baz"]
// ------------------
// pass arrow function as 2nd argument
getScript('https://bit.ly/get-script-example', () => {
console.log('The script load is done.');
});
// Hello, I am a remote script that successfully loaded.
// The script load is done.
// ["foo", "bar", "baz"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment