Created
July 30, 2017 17:58
-
-
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
This file contains hidden or 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 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