Noob - learning event handling with window.onload
Hello, learning javascript.
This code is supposed to handle different functions when the window onloads.
In the code initOne, initTwo and initThree are functions.
:Questions:
Don't really see how window.onload can store functions?
It seems to be more of an event.
And if it is an event, why is it that the functions stored in the oldOnload variable are not executed the scond time the addOnLoad function is called in line 2?
Also, how can oldOnload be a conditional if it only seems to be an array of functions? ---->*if (oldOnload) {}
:confused:
Code:
addOnLoad(initOne);
addOnLoad(initTwo);
addOnLoad(initThree);
function addOnLoad(newFunction){
var oldOnload = window.onload;
if(typeof oldOnload =="function"){
window.onload = function() {
if (oldOnload) {
oldOnload();
}
newFunction();
}
}
else{
window.onload = newFunction;
}
}
I think this is what's happening
Code:
this.functions=new Array();
This makes a new array in preparation for storing functions
Code:
this.attachEvent=function(f){
this.functions[this.functions.length]=f;
}
This makes a method for this that appends function f onto the bottom of the array if they don't want to do something like
this.functions.push(f);
Then they can use the array of functions for something.