Preface
Some functions must be executed after the web page is loaded. For example: involving DOM operations.
When the web page is loaded, an onload event will be triggered, and the function will be bound to this event.
The code copy is as follows:
window.onload = myFunction;
The question is: If multiple events need to be bound at the same time, how should we deal with it? There are two solutions
Plan 1
Create an anonymous function to accommodate multiple events that need to be bound, and then bind this anonymous function to the onload event
window.onload = function(){firstFunction();secondFunction();...... }Plan 2
The addLoadEvent function written by Simon Willsion:
function addEventLoad(func){var oldOnload = window.onload;if(typeof window.onload != 'function'){window.onload = func;}else{window.onload = function(){oldOnload();func();}} }Store the value of the existing window.onload event handler function into the variable oldOnload
If the function is not bound to this processing function, then bind the new function to it like that.
If a function has been bound, the new function is appended to the end of the instruction.
Calling method:
addEventLoad(firstFuction);
addEventLoad(secondFuction);
The above content is the method of binding multiple events to window.onload through two solutions. I hope it will be helpful to everyone!