With Javascript, I’m wondering what’s the current best way to load my code onto the page (without jQuery) after the rest of the page has been loaded. So like a window.onload…
I was looking at several options but they all seem outdated and not-completely cross-browser friendly.
Does this look like it would be best? It does work, I got it from online tools but I think it spawned from Scott Andrews
function addEvent(obj, evType, fn) {
if (obj.addEventListener){
obj.addEventListener(evType, fn, false);
return true;
} else if (obj.attachEvent){
var r = obj.attachEvent("on"+evType, fn);
return r;
} else {
return false;
}
}
function init () {
// do something
}
addEvent(window, 'load', init);
Even though it works and passes all my tests and seems quite flexible, I’m a little worried about this approach, because
-
I don’t fully understand it
-
It seems old, Scott Andrews page is from 2001!
-
Opera Technical support didn’t like it, see “The voice of Opera” on online tools
Is this the best way to do this?
From The Javascript Source