Window. onload is often used for projects, but window. onload cannot load multiple functions at the same time. The usage is as follows:
Function func () {alert ("this is window onload event! "); Return ;}
Window. onload = func;
Or:
Window. onload = function () {alert ("this is window onload event! "); Return ;}
However, window. onload cannot load multiple functions at the same time.
For example:
The Code is as follows:
Function t (){
Alert ("t ")
}
Function B (){
Alert ("B ")
}
Window. onload = t;
Window. onload = B;
Later, we will overwrite the previous one. The above code will only output B.
You can solve the problem as follows:
Window. onload = function () {t (); B ();}
Another solution is as follows:
The Code is as follows:
Function addLoadEvent (func ){
Var oldonload = window. onload;
If (typeof window. onload! = 'Function '){
Window. onload = func;
} Else {
Window. onload = function (){
Oldonload ();
Func ();
}
}
}
Use:
The Code is 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 ();}
I personally think that implicit functions (such as window. onload = function () {t (); B (); c () ;}) is faster. Of course, you can use addLoadEvent for more professional purposes!