JavaScript: How can I delay running some JS code until ALL of my asynchronous JS files downloaded?
- by Henryh
UPDATE:
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 both Two.js & Three.js has been downloaded and excuted
</script>
How can I prevent code from executing until all required JS have been downloaded and executed? In my example above, those required files being Two.js and Three.js.