Replacement for script onload in IE
Firefox and Safari support an onload event for SCRIPT elements. That is, you can dynamically add a new script to a page, set its onload event to fire a callback, and know when the script has been successfully loaded. You would want to do this because when you include a bunch of new SCRIPT tags in a page, there are no guarantees in what order the browser will decide to evaluate them, thus making dependencies among the scripts difficult to resolve. Using onload to chain the script additions is one solution to this, however, Internet Explorer doesn't seem to support onload in SCRIPT tags. Here is a workaround:
function importJS(src, look_for, onload) {
var s = document.createElement('script');
s.setAttribute('type', 'text/javascript');
s.setAttribute('src', src);
if (onload) wait_for_script_load(look_for, onload);
var head = document.getElementsByTagName('head')[0];
if (head) {
head.appendChild(s);
} else {
document.body.appendChild(s);
}
}
function importCSS(href, look_for, onload) {
var s = document.createElement('link');
s.setAttribute('rel', 'stylesheet');
s.setAttribute('type', 'text/css');
s.setAttribute('media', 'screen');
s.setAttribute('href', href);
if (onload) wait_for_script_load(look_for, onload);
var head = document.getElementsByTagName('head')[0];
if (head) {
head.appendChild(s);
} else {
document.body.appendChild(s);
}
}
function wait_for_script_load(look_for, callback) {
var interval = setInterval(function() {
if (eval("typeof " + look_for) != 'undefined') {
clearInterval(interval);
callback();
}
}, 50);
}
(function(){
importCSS('some_stylesheet.css');
importJS('jquery-1.2.6.js', 'jQuery', function() {
importJS('some_script_that_uses_jquery.js');
importJS('another_one.js', 'SomeClassOrVariableSetByTheScript',
function() {
importJS('a_script_that_uses_that_class_or_variable.js');
});
});
})();
November 25th, 2008 at 4:51 pm
Thanks for your post, but I have since found a more efficient way to do this and thought I might share with you. As you said you can use an onload event in Firefox, but in IE you can use the onreadystatechange event. Works from at least IE6. Haven’t tested earlier.
November 25th, 2008 at 8:03 pm
Thanks for the advice, that’s good to know about!
February 28th, 2009 at 7:10 pm
Last week I wrote my first Greasemonkey script. I then made a bookmarklet with the same functionality, but I couldn’t paste in minified jQuery. not enough room.
My solution was to create a script element, set its src to jquery.js, then give it an eventListener and callback for the load event. This worked fine in Safari and Firefox, but I couldn’t figure out the problem for IE. I hate IE and just left it at that.
A week later, I’m browsing twitter, see a tweet about SelectorGadget (awesome btw), and finally land on your blog, where I learn that IE doesn’t support the load event for scripts. so, i used your set/clear interval solution and bam…my bookmarklet now works for the gazillions of internet explorer users. Thanks.
btw, the bookmarklet/gm script’s adds expand/minimize functionality to the title bar of iGoogle Gadgets. it’s located here: http://googlegadgetblog.com/features/expand-minimize-bookmarklet-igoogle-gadgets/
March 11th, 2009 at 5:03 am
Stumbled across these post’s while looking for a solution for an onload issue I’m having…
I’m using the following script to keep vertical CSS sliding navigation categories open while the user is on certain pages of my site;
var thisPageMenu = (’smenu1′);
window.onload = show(thisPageMenu);
This works great in Safari and Firefox, but not IE. Can your workaround be applied to this?
Any advice would be much apprectiated.