Window.onload binding Multiple Events-two solutions

Source: Internet
Author: User

Objective

Some functions must be executed after the page has been loaded. For example: DOM manipulation is involved.

When the page is loaded, an onload event is triggered and the function is bound to the event.

Window.onload = myFunction;

The question is: what if you need to bind more than one event at a time? There are two ways to solve this problem

Programme I

Create an anonymous function to accommodate multiple events that need to be bound, and then say that the anonymous function is bound to the OnLoad event

1 function () {2   firstfunction (); 3    secondfunction (); 4     5 }

Programme II

the Addloadevent function written by Simon Willsion:

1 functionAddeventload (func) {2    varOldonload =window.onload;3    if(typeofWindow.onload! = ' function '){4Window.onload =func;5}Else{6Window.onload =function(){7 oldonload ();8 func ();9         }Ten    } One  A}
    1. The value of the existing Window.onload event handler is stored in the variable oldonload
    2. If there is no binding function on the handler, then bind the new function to it.
    3. If the function is already bound, the new function is appended to the end of the instruction.

Call Method:

Addeventload (firstfuction);

Addeventload (secondfuction);

Window.onload binding Multiple Events-two solutions

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.