Download jQuery, wait

I need to dynamically load jQuery and jQuery UI from javascript and then check if it loaded and do something later.

function loadjscssfile(filename, filetype){ if (filetype=="js"){ //if filename is a external JavaScript file var fileref=document.createElement('script'); fileref.setAttribute("type","text/javascript"); fileref.setAttribute("src", filename); } else if (filetype=="css"){ //if filename is an external CSS file var fileref=document.createElement("link"); fileref.setAttribute("rel", "stylesheet"); fileref.setAttribute("type", "text/css"); fileref.setAttribute("href", filename); } if (typeof fileref!="undefined") document.getElementsByTagName("head")[0].appendChild(fileref); } loadjscssfile("http://localhost/js/jquery-1.3.2.min.js", "js"); loadjscssfile("http://localhost/js/jquery-ui-1.7.2.custom.min.js", "js"); 

I did some research and found that I need to either use a callback or set. The problem is that I'm really new to javascript, and that really confuses me. Can someone tune me in the right direction?

+4
source share
2 answers

I never had to do this on my own, but presumably you could just use a repeating timeout to check for the presence of the required objects:

 function jqueryLoaded() { //do stuff } function checkJquery() { if (window.jQuery && jQuery.ui) { jqueryLoaded(); } else { window.setTimeout(checkJquery, 100); } } checkJquery(); 
+19
source

I am sure that the window.onload () function should be run when all scripts are loaded. And you do not need to bind things to the " ready " event in jQuery.

 loadjscssfile("http://localhost/js/jquery-1.3.2.min.js", "js"); loadjscssfile("http://localhost/js/jquery-ui-1.7.2.custom.min.js", "js"); window.onload = function() { if(window.jQuery && jQuery.ui) { alert('loaded'); } } 
+1
source

All Articles