JavaScript: How can I delay running some JS code until my JS file downloaded?

Posted by Henryh on Stack Overflow See other posts from Stack Overflow or by Henryh
Published on 2010-05-10T04:33:01Z Indexed on 2010/05/10 4:38 UTC
Read the original article Hit count: 269

I have the following code:

<script type="text/javascript">
function addScript(url) {
    var script = document.createElement('script');
    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
}   
addScript('http://example.com/One.js');
addScript('http://example.com/Two.js');
addScript('http://example.com/Three.js');
addScript('http://example.com/Four.js');

...

// run code below this point once Two.js has been downloaded and excuted

</script>

How can I detect when one of my JavaScript files has been downloaded and executed so that I can use it?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about web-development