I'm writing an engine that requires the use of getScript quite extensively. I've pushed it into its own function, for ease of use, but now I need to make sure that the function itself is synchronous. Unfortunately, I can't seem to make getScript wait until the script it loads is actually finished loading before proceeding. I've even tried setting jQuery's ajax asynch property to false before making the call. I'm thinking of using jQuery's when/done protocol, but I can't seem to wrap my head around the logic of placing it inside a function and making the function itself synchronous. Any help would be very much appreciated!
function loadScript(script){ //Unrelated stuff here!!! $.when( $.getScript(script,function(){ //Unrelated stuff here!!! })).done(function(){ //Wait until done, then finish function }); }
Loop code (by request):
for (var i in divlist){ switch($("#"+divlist[i]).css({"background-color"})){ case #FFF: loadScript(scriptlist[0],divlist[i]); break; case #000: loadScript(scriptlist[2],divlist[i]); break; case #333: loadScript(scriptlist[3],divlist[i]); break; case #777: loadScript(scriptlist[4],divlist[i]); break; } }
This worked for me, and may help you.
$.ajax({ async: false, url: "jui/js/jquery-ui-1.8.20.min.js", dataType: "script" });
Basically, I just bypassed the shorthand notation and added in the async: false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With