Skip to content

Instantly share code, notes, and snippets.

@richard512
Created July 30, 2017 17:58
Show Gist options
  • Save richard512/b4cf0b6de5d56eb318531abbaf279725 to your computer and use it in GitHub Desktop.
Save richard512/b4cf0b6de5d56eb318531abbaf279725 to your computer and use it in GitHub Desktop.
loadScript -- load javascript file from javascript and run a callback function after it's loaded
function loadScript(url, callback)
{
// adding the script tag to the head as suggested before
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
// then bind the event to the callback function
// there are several events for cross browser compatibility
script.onreadystatechange = callback;
script.onload = callback;
// fire the loading
head.appendChild(script);
}
var jqueryIsLoaded = function() {
alert("jqueryIsLoaded")
};
loadScript("https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js", jqueryIsLoaded);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment