Usually, it is often necessary to use Window.onload,
The usage is 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 coverage will be covered, and the above code will only output B.
At this time, the following method can be used to solve:
window.onload = function () {t (); b (); b ();}
Another solution is as follows:
Copy code code as follows:
Function addloadEvent (func) {
Var Oldonload = Window.Onload; // Get the function of the previous online event
if (typeof Window.onload! = 'Function') {// Determine whether the type is 'Function', note that Typeof returns the string
Window.onload = Func;
} Else {
window.onload = function () {
Oldonload (); // The function of the onLoad event covered by the previously covered ----> Since I don't know much about JS, I temporarily understand it as a function that covers the ONLOAD event to achieve multiple functions loading multiple functions
func (); // Call the current event function
}
}
}
// (Complete examples) Use as follows:
Function t () {
alert ("t")
}
function b () {
Alert ("B")
}
Function C () {
Alert ("C")
}
Function addloadEvent (func) {
var omoad = window.onload;
ifof Window.onload! = 'Function') {{
Window.onload = Func;
} Else {
window.onload = function () {
Oldonload ();
func ();
}
}
}
addloadEvent (t);
addloadEvent (b);
addloadEvent (c);
// Wait at window.onload = function () {t (); b (); c (); c ();}
Personally, I think that using hidden functions (such as: Window.onload = Function () {t (); b (); c ();}) Faster, of course, use the adDloadEvent to be more professional, take it!
Js window.onload addition function:
Copy code code as follows:
<script>
If (Window.attachevent) // IE: If the Window.attachevent function exists in the browser, use the window.attachevent function to determine whether IE can also use: if (document.all) {// ..}
Window.attachevent ("OnLoad", Function () {Alert ("Add Method");});});
else // firefox
Window.adDeventListener ("load", function () {alert ("adD method");}, true);
</script>
Run, the message pops up in JS, the problem is solved.
=========== Relevant information ===============
Attachevent binds the specified function to the event so that the function is called whenever the event is triggered by the object.
The Internet Explorer provides an ATTACHEVENT method from 5.0. Using this method, you can assign multiple processing processes for an event. Attachevent is also applicable to the current Opera. But Mozilla/Firefox does not support this method. But it supports another AddeventListener method. This method is similar to Attachevent, and it is also used to assign multiple processing processes for one event. However, there are some differences they assigned events. In the Attachevent method, the event starts with "on", and in AddeventListener, the incident does not start "on". In addition That's fine.