javascript - How to dynamically append JS/CSS files in order -
so, have load these in main page
jquery.js jquerymobile.css jquerymobile.js mainscript.js
but if user enters anywhere else need load them dynamically. have
scriptcheck.js
if scripts not present //create array of elements containing above files scriptsarray.foreach |script| head.append(script) end foreach end if
my question is, there way make loading synchronous operation, waits each script load , interpreted before loading next one?
you set onload handler of each script element function loads next script:
var counter = 0 scriptpaths = [...] function loadnextscript(){ var el = document.createelement("script"); el.onload = loadnextscript; el.src = scriptpaths[counter]; document.head.appendchild(el); counter++; }
you can invoke using:
if (scriptsnotpresent) { loadnextscript(); }
Comments
Post a Comment