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:CopyCodeThe 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:Copy codeThe 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:Copy codeThe 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!