I often need to use window.onload when doing projects,
is used as follows:
function func(){alert("this is window onload event!");return;}
window.onload=func;
or as follows:
window.onload=function(){alert("this is window onload event!");return;}
But window.onload cannot load multiple functions at the same time.
For example:
function t(){
alert("t")
}
function b(){
alert("b")
}
window.onload =t ;
window.onload =b ;
The previous one will be overwritten later, and the above code will only output b.
The following method can be used to solve this problem:
window.onload =function() { t(); b(); }
Another solution is as follows:
//(Full example) is used as follows:
function t(){
alert("t")
}
function b(){
alert("b")
}
function c(){
alert("c")
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
addLoadEvent(t);
addLoadEvent(b);
addLoadEvent(c);
//Equivalent to window.onload =function() { t(); b(); c( ) ;}
JS window.onload append function:
============Related information================
attachEvent Binds the specified function to an event so that the function is called whenever the event fires on the object.
Internet Explorer has provided an attachEvent method since 5.0. Using this method, you can assign multiple processing processes to an event. attachEvent also works for current Opera. But Mozilla/Firefox does not support this method. But it supports another addEventListener method, which is similar to attachEvent and is also used to assign multiple handlers to an event. But there are some differences in the events they assign. In the attachEvent method, the event starts with "on", but in addEventListener, the event does not start with "on". In addition, addEventListener has a third parameter. Generally, this parameter is specified as false. That's it.