Window. onload is often used for projects,
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:
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:
Function addloadevent (func) {var oldonload = Window. onload; // obtain the function of the previous onload event if (typeof window. onload! = 'Function') {// determines whether the type is 'function'. Note that typeof returns the string window. onload = func;} else {window. onload = function () {oldonload (); // call the previously overwritten onload event function ----> because I do not know much about JS, here, I temporarily understand that the function that overwrites the onload event is used to load multiple functions func (); // call the current event function }}// (complete example) using the following: 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 ();}
If (window. attachevent) // ie: If the Browser contains window. the attachevent function uses window. the attachevent function can also be used to determine whether it is ie: If (document. all ){//..} window. attachevent ("onLoad", function () {alert ("add method") ;}); else // firefoxwindow. addeventlistener ("LOAD", function () {alert ("add method") ;}, true );