JavaScript: How can I delay running some JS code until my JS file downloaded?
- by Henryh
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?