jquery $ (document). Ready () method and Window.onload method

Source: Internet
Author: User

jquery $ (document). Ready () method and Window.onload method

In most cases, the jquery code is written that requires us to put the jquery code in the

$ (document). Ready (function () {

... jquery code ........ .....

});

The above code and

$ (). Ready (function () {

... jquery code ........ .....

});
$ (function () {

... jquery code ........ .....

});

is equivalent ~ ~ The last way is the most concise way, most of the cases I use the last one, hehe ~ ~ Good remember!
Note: Sometimes our own jquery code has a baffling problem, seemingly normal code, but it is in the run-time error, then please check your code is not placed in the $ (document). Ready () method runs.

Let's say $ (document). The difference between the ready () method and the Window.onload method:
Their main difference has two points:

Timing of execution
The Window.onload method is not executed until all elements in the Web page, including all associated files of the element, are fully loaded into the browser. The event handler that is registered through the $ (document). Ready () method in jquery can be invoked as long as the DOM is fully ready, such as a picture as long as the tag is complete, without waiting for the picture to load. You can set a picture's width-high properties or styles, and so on.
$ (document). The Ready () method can be used multiple times to register a different event handler, and Window.onload can only save a reference to one function at a time, and a multiple-bound function will only overwrite the preceding function.
Let's look at the results of the Window.onload method registering two times on a page:

function One () {

Alert ("one");

}

function two () {

Alert ("two");

}

Window.onload = two;

Window.onload = one;

When the above code is run, "one" pops up.


Let's take a look at $ (document). Ready () method is called two times what the result will be.

function One () {

Alert ("one");

}

function two () {

Alert ("two");

}

$ (document). Ready (function () {

One ();

});

$ (document). Ready (function () {

Two ();

});

When the above code runs, it pops up "one" and "two" respectively.

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.