Last active
June 23, 2021 22:03
-
-
Save kevinchisholm/e0f01f4c18b13de06c0d2922e9445a0a to your computer and use it in GitHub Desktop.
jQuery.getScript Alternatives - Example 4
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 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