Does setting an onload event for a <script> tag work consistently in modern browsers?
- by Matchu
I observe that placing the following in an external script file has the desired effect in my copies of Firefox and Google Chrome:
var s = document.createElement('script');
s.setAttribute('type', 'text/javascript');
s.setAttribute('src', 'http://www.example.com/external_script.js');
s.onload = function () { doSomethingNowThatExternalScriptHasLoaded }
document.getElementsByTagName('body')[0].appendChild(s);
It adds a an external script tag to them DOM, and attaches a function to the tag for when the script has loaded.
I'm having trouble testing in Internet Explorer right now, but I'm not sure if it's related to that addition in particular, or something else. Does this method work in the more modern versions of other browsers, including IE7/8? If not, how else could I go about this?