How to know if JQuery has finished loading
javascript, jquery
Solution
You will have to write code to detect when a dynamically loaded script is loaded and unfortunately, it works slightly different in some older browsers so it isn't as simple as it could be. Here's a good reference article on how to do that: http://www.ejeliot.com/blog/109
Here's some of the code from that article:
function loadScript(sScriptSrc, oCallback) {
var oHead = document.getElementsByTagName('head')[0];
var oScript = document.createElement('script');
// make sure callback isn't run more than once
function runCallback() {
if (oCallback) {
oCallback();
oScript.onload = oScript.onreadystatechange = null;
oCallback = null;
}
}
oScript.type = 'text/javascript';
// most browsers
oScript.onload = runCallback;
// IE 6 & 7
oScript.onreadystatechange = function() {
if (this.readyState === 'complete') {
runCallback();
}
}
oScript.src = sScriptSrc;
oHead.appendChild(oScript);
}
Alternatively, you could use one of the tiny libraries that does this work for you. You can see a list of some of those here: http://microjs.com/#loader. Though I'm not sure you want to use yet another library to assist with loading your primary library.
Problem
For some case say I need to load jQuery using a piece of javascript like this : ``` <script type="text/javascript"> if (typeof jQuery == 'undefined') { var script = document.createElement('script'); script.type = "text/javascript"; script.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"; document.getElementsByTagName('head')[0].appendChild(script); } </script> ``` But how will I know that JQuery has finished loading so that I can use it.