Javascript Window.Onload Function Chain
Just for experimentation, I was trying to figure out different ways of non-destructive functionality window.onload
in a web browser. This is the idea for what I have so far:
var load = window.onload;
var newFunction = function(){
alert("ha!");
}
window.onload = function(){
load();
newFunction();
}
The problem I see with this is that every time you bind a function, it adds another layer of function calls to the stack. Is there a better way to do this without adding extra depth to the call stack?
+1
source to share
3 answers
You can see jQuery how they handle this.
From the jQuery docs :
You can have as many (. Document) .ready events on your page as possible. The functions are then executed in the order in which they were added.
0
source to share
I would rather use a "good enough" addEvent:
var addEvent = function( obj, type, fn ) {
if (obj.addEventListener)
obj.addEventListener(type, fn, false);
else if (obj.attachEvent)
obj.attachEvent('on' + type, function() { return fn.apply(obj, new Array(window.event));});
}
From: http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/
0
source to share