Execute multiple JS functions after page loading

Source: Internet
Author: User

Usually we need to load the script when the page is opened, which must be executed after the page has been loaded, because the DOM is complete and can be used by window.onload to ensure this, such as:
Window.onload=firstfunction;
This script means to execute the Firstfunction function after the page is finished, but when there are many functions that need to be executed when the page is loaded? Some may say that this is possible:
Window.onload=firstfunction;
Window.onload=secondfunction;

However, this will only execute the Secondfunction function.

Simon Willison Blog offers the perfect solution:

[JavaScript]View PlainCopy
  1. function Addloadevent (func) {
  2. var oldonload = window.onload;
  3. if (typeof window.onload! = ' function ') {
  4. Window.onload = func;
  5. } Else {
  6. Window.onload = function () {
  7. if (oldonload) {
  8. Oldonload ();
  9. }
  10. Func ();
  11. }
  12. }
  13. }

The func parameter is the name of the function to be loaded (remember only the function name, not the parentheses), how many functions need to be executed after the page load, and the number of times the above classic function is called, for example:
Addloadevent (firstfunction);
Addloadevent (secondfunction);
......

Execute multiple JS functions after page loading

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.