i'm trying to load Javascript files that a local js file dependent on them .. the files are external on a website is there a way to load all dependencies at once
Asked
Active
Viewed 80 times
2 Answers
1
You can do it with jQuery
$.getScript("my_lovely_script.js", function(){
alert("Script loaded and executed.");
// here you can use anything you defined in the loaded script
});
Hope this helps

felipekm
- 2,820
- 5
- 32
- 42
0
I usually load jquery in the head section, then I load the remaining scripts with dependencies inside $(document).ready(). This script ensures to load every js one by one in a synchronous way to avoid conflicts.
var $apis =[
"/assets/js/script1.js",
"/assets/js/script2.js"
];
var deferred = new $.Deferred(), step = deferred;
$.each($apis, function(i, val) {
step = step.then(function() {
return $.getScript(val, function() {
if(i==$apis.length-1) {
// initialize here after all scripts are loaded
}
});
});
});
deferred.resolve();

cardeol
- 2,218
- 17
- 25
-
ok let me show you the script i'm try to load it may have something wrong int cause it still give me exception with your answer https://s3-us-west-2.amazonaws.com/com.jumbotron.javascript/jquery.jumbotronmediaplayer.js – Mohamed Emad Hegab Jan 15 '14 at 15:26
-
check this Plunker example http://plnkr.co/edit/WwAI4zYlIKa2IwII4WgQ – cardeol Jan 15 '14 at 15:51