Javascript example Tutorial: sharing onload events

Source: Internet
Author: User

Assume two functions: firstFunction and secondFunction. What should I do if I want these two functions to be executed during page loading? If they are gradually bound to the onload event, they will be executed only when the last one is:

Window. onload = firstFunction;

Window. onload = secondFunction;

SecondFunction will replace firstFunction. You may think that each event processing function can only bind one command.

There is one way to avoid this problem: You can first create an anonymous function to accommodate the two functions, and then bind the anonymous function to the onload event, as shown below:

Window. onload = function (){

FirstFunction ();

SecondFunction ();

}

He does have a good job-it should be the simplest solution when not many functions need to be bound.

There is also an best elasticity solution-no matter how many functions you plan to execute when the page is loaded, it can be freely applied. This scheme requires additional code, but the advantage is that once the code is available, it is very easy to bind the function to the window. onload event.

The following are the operations to be completed by the addLoadEvent function.

Store the values of the existing window. onload event handler function to the variable oldonload.

If no function is bound to this processing function, add the new function to it as usual.

If some functions have been bound to this processing function, append the new function to the end of the existing command.

Function addLoadEvent (func ){

Var oldonload = window. onload;

If (typeof window. onload! = 'Function '){

Window. onload = func;

} Else {

Window. onload = function (){

Oldonload ();

Func ();

}

}

}

This will create a queue for the functions executed when the page is loaded. To add the two functions to the queue, write the following code:

AddLoadEvent (firstFunction );

AddLoadEvent (secondFunction );

This function is very practical, especially when the amount of code becomes more and more complex. No matter how many functions are executed when the page is loaded, you can arrange everything by writing one more statement.

Related Article

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.