Objective
Some functions must be executed after the page has been loaded. For example: DOM manipulation is involved.
When the page is loaded, an onload event is triggered and the function is bound to the event.
Window.onload = myFunction;
The question is: what if you need to bind more than one event at a time? There are two ways to solve this problem
Programme I
Create an anonymous function to accommodate multiple events that need to be bound, and then say that the anonymous function is bound to the OnLoad event
1 function () {2 firstfunction (); 3 secondfunction (); 4 5 }
Programme II
the Addloadevent function written by Simon Willsion:
1 functionAddeventload (func) {2 varOldonload =window.onload;3 if(typeofWindow.onload! = ' function '){4Window.onload =func;5}Else{6Window.onload =function(){7 oldonload ();8 func ();9 }Ten } One A}
- The value of the existing Window.onload event handler is stored in the variable oldonload
- If there is no binding function on the handler, then bind the new function to it.
- If the function is already bound, the new function is appended to the end of the instruction.
Call Method:
Addeventload (firstfuction);
Addeventload (secondfuction);
Window.onload binding Multiple Events-two solutions