I need to load some js files dynamically and sequentially (i.e. the second script load after loading the full first, third after second, etc.).
Question: how was it discovered when the script was loaded? I ran into problems with the onload event - it does not fire in IE8. After reading this , I tried subscribing to onreadystatechange and wrote very ugly code to load the script:
function loadScript(url, callback) { var isLoaded = false; var script = document.createElement('script'); script.onreadystatechange = function () { if ((script.readyState == 'complete' || script.readyState == 'loaded') && !isLoaded) { if (callback) callback(); } }; script.setAttribute('type', 'text/javascript'); script.setAttribute('src', url); document.head.appendChild(script); };
Can you offer the best cross browser solution without such tricks?
UPD: Thanks for the answers. What if I need to download jquery.js (for example, the client has an old version) :)?
2xMax source share