At present, I have a javascript callable functions (without adding global variables) that add scripts to the head.
Onload is useful for pages after the site loaded initially, but I'm waiting to load larger scripts until they're needed considering many won't need them.
The following code works for some scripts, but not for some larger ones...
function include(script) {
var head= document.getElementsByTagName('head')[0];
var newScript= document.createElement('script');
newScript.type= 'text/javascript';
newScript.src= script;
head.appendChild(newScript);
}
function doThething(){
include('foo');
include('bar');
if(document.readyState === "complete") {
// do something needing foo and bar
} else {
window.addEventListener("onload", function () {
// do something needing foo and bar
}
}
}
With some scripts, the above works fine and dandy. With others (especially largers ones that client doesn't have)it tries to execute before foo has loaded. I can't change foo because it's a larger off-site public library.
How can I make it make the dom wait for the new stuff to finish loading and kick off an onload again, or otherwise ensure that foo is loaded before I doTheThing?