JS Window.onload method of loading multiple functions _javascript skills

Source: Internet
Author: User
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;}

However, window.onload cannot load multiple functions at the same time.

Like what:
Copy Code code as follows:

function T () {
Alert ("T")
}
Function B () {
Alert ("B")
}
Window.onload =t;
Window.onload =b;

The previous overlay will be followed, and the above code will only output B.
The following methods can be used to resolve this:

Window.onload =function () {t (); B ();}

The other solution is as follows:
Copy Code code 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 following:
Copy Code code 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 ();}

Individuals assume that implicit functions (such as: Window.onload =function () {t (); B (); C ();}) are used directly. Faster, of course, use addloadevent more professional, each take the good!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.