How to call js when jquery loads a page

Source: Internet
Author: User

The following describes how to call js when loading a page:

Window. onload = function (){
$ ("Table tr: nth-child (even)"). addClass ("even"); // jquery code
};

This code will be executed after all the documents on the entire page are loaded. Unfortunately, this method not only requires that the DOM tree of the page be fully loaded, but also requires that all external images and resources be fully loaded. Unfortunately, if external resources and sample files take a long time to load, this js effect will make the user feel ineffective.

But the jquery method is used:

$ (Document). ready (function (){

// Any js special effects to be executed
$ ("Table tr: nth-child (even)"). addClass ("even ");
});

You only need to load all the DOM structures and execute the js effect before the browser puts all the HTML into the DOM tree. Including before loading external images and resources.

There is also an abbreviated method:

$ (Function (){

// Any js special effects to be executed
$ ("Table tr: nth-child (even)"). addClass ("even ");
});

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.