I have the following function that loads a given script :
function addScriptTag(url){
var script = document.createElement('script');
script.src = url;
document.getElementsByTagName('head')[0].appendChild(script);
}
I used the function to load libs correlated to each other where lib2 depends on lib1 and lib1 depends on jquery :
function loadThemAll(){
addScriptTag('http://path/to/jquery.js');
addScriptTag('http://path/to/lib1.js');
addScriptTag('http://path/to/lib2.js');
}
The problem is that even in that order, when lib1 needs jQuery, it doesn't find it. The same applies for lib2 that uses lib1.
How can I make script tag creates and load the script sequentially ? In other words :
add script tag for jQuery and load it.
When jQuery is loaded, add script tag for lib1 and load it.
When lib1 is loaded, add script tag for lib2 and load it.