JS in the execution of the page after the completion of several methods and execution sequence

Source: Internet
Author: User

1: $ (function) {} with jquery;

2: Use jquery's $ (document). Ready (function () {}); The first two are essentially no different, and the 1th is the abbreviated way of the 2nd kind. The two are the methods that are executed when the document is loaded.

3: Use jquery's $ (window). Load (function () {});

4: Using window.onload = function () {} The 3rd and 4th are waited until the entire window is loaded to complete the execution method body. There is no difference between the two, just one using a DOM object and one using a jquery object.

5: The onload event is statically bound on the label, <body onload= "aaa ()" > waits for the body load to complete, the AAA () method is executed.

So, what is the order of the five ways to execute?

Verify the discovery with the code below:

Use 1:jquery's $ (function) {} and 2:jquery $ (document). Ready (function () {}); no matter where the position is placed, The remaining three methods are always preferred (the reason is that these two methods are executed after the document is loaded, the latter three are not executed until the entire window page is loaded), and the order in which they are executed is the top priority.

Use 3:jquery's $ (window). Load (function () {}), and 4:window.onload = function bbb () {}, which always takes precedence over <body onload= "aaa ()" > Perform. They both execute the order also according to who is above who first executes.

Use 5:<body onload= "aaa ()" > Always last executed.

here's the point! These are not useful for IE browsers if you want to operate on future elements. to wait for the future elements to be loaded after the execution of JS, we will give a timer to achieve the effect of delayed loading:

$ (window). Load (function () {setTimeout () {Console.log ($ (". Tbody tr")) $ (". Tbody tr:odd"). CSS ("Backgroundcol  or "," #F5F6FA "); },30)});

  

JS in the execution of the page after the completion of several methods and execution sequence

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.