I have one paragraph of javascript code. And i don't understand it very well. Can you expain it line by line for me? Thanks a lot.
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
And here is what i'm thinking:
function addLoadEvent(func) { //define a function with a parameter 'func'
var oldonload = window.onload; //assign window.onload event to variable oldonload
if (typeof window.onload != 'function') { //if window.onload is not a function, then...
window.onload = func; //assign 'func' to window.onload event. what does func mean?
} else { //if window.onlad is a function
window.onload = function() { //don't understand
oldonload(); //call function oldonload()
func(); //call function func()
}
}
}
Confusions:
window.onload is already an event, and why do we use typeof?
function addLoadEvent(func) , window.onload = func, func(). What's the difference among these funcs?
I'm sorry for posting a novice problem. But thanks to anyone who gives me some guidance?
Edit:
This is improved original code by Simon Willison.
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
onload is most often used within the <body> element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.).
ready() is a jQuery event which occurs when the HTML document has been fully loaded, while the window. onload event occurs later, when everything including images on the page loaded. Also window. onload is a pure javascript event in the DOM, while the $(document).
The general idea is that window. onload fires when the document's window is ready for presentation and document. onload fires when the DOM tree (built from the markup code within the document) is completed.
Unfortunately, you cannot place multiple onload events on a single page. You can nest multiple functions within the one onload call, but what if you need to cascade an onload script across multiple pages, some which may already have an existing onload event? use the addLoadEvent function below.
Its very simple.
You check if there is already an onload function registered.
if there isnt one, 1. assign 'func' the function you pass in to the onload
if there is one, create a new onload function, that will:
and:
window.onload is already an event, and why do we use typeof?
window.onload can be a function, but if one isnt set, it will be 'undefined' we need to check its type to see what it is.
function addLoadEvent(func) , window.onload = func, func(). What's the difference among these funcs?
functions are a variable in javascript. so you can refer to the function as
func
you can call it as
func()
in your case: function addLoadEvent(func) is the current function defintion. it takes one param, and that param should be a function
window.onload = func assigns the function you passed in to the onload event
func()
calls the function you passed in
here is the line by line correction:
function addLoadEvent(func) { //define a new function called addLoadEvent which takes in one param which should be function
var oldonload = window.onload; //assign window.onload event to variable oldonload
if (typeof window.onload != 'function') { //if window.onload is not a function, and thus has never been defined before elsewhere
window.onload = func; //assign 'func' to window.onload event. set the function you passed in as the onload function
} else { //if window.onlad is a function - thus already defined, we dont want to overwrite it so we will..
window.onload = function() { //define a new onload function that does the following:
oldonload(); //do whatever the old onload function did
func(); //and then do whatever your new passed in function does
}
}
}
//then call it like this:
addLoadEvent(function() {
alert("hi there");
});
addLoadEvent(function() {
alert("this will be alerted after hi there");
});
//or like this:
var fn = function(){
alert("this will be the last thing alerted");
};
addLoadEvent(fn);
Can you expain it line by line for me?
I'm too lazy, but the nutshell is:
window.onload = func; //assign 'func' to window.onload event. what does func mean?
func is the variable defined as the first argument to the function (function addLoadEvent(func)). Functions are first class objects in JavaScript — you can pass them around, just like any other variable.
window.onload is already an event, and why do we use typeof?
It isn't an event. It is a property that might be a function or undefined. The event is 'something that happens' which causes that function to be run.
function addLoadEvent(func) , window.onload = func, func(). What's the difference among these funcs?
The first defines a function named addLoadEvent. The second assigns a function named func to onload. The third calls a function named func.
As an aside, don't use this. Use addEventListener/attachEvent or a library that normalises them across browsers such as YUI or jQuery.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With