jquery $ (document). The difference between ready () and window.onload _javascript skills

Source: Internet
Author: User

jquery $ (document). Ready () is similar to the Window.onload method in traditional JavaScript, but it differs from the Window.onload method.

1. Execution time
Window.onload must wait until all elements of the page including the picture have been loaded before they can be executed.
$ (document). Ready () is executed after the DOM structure has been drawn and does not have to wait until the load has finished.
2. Different number of writes
Window.onload cannot write multiple at the same time, if there are multiple window.onload methods, only one
$ (document). Ready () can be written multiple at the same time and can be executed
3. Simplified wording
Window.onload did not simplify the wording
$ (document). Ready (function () {}) can be simply written as $ (function () {});

In my previous development, I used JavaScript in general, and I used the pattern of jquery, which is most of the time, the first line is written:

Copy Code code as follows:

$ (document). Ready (function () {
...
});

This time, not necessarily wait for all the JS and picture loaded, you can execute some methods, but sometimes, must wait for all the

Elements are loaded before you can execute some methods, for example, some pictures or other aspects have not been loaded well, this time, click on some buttons, will cause an unexpected situation, this time,

Need to use:

Copy Code code as follows:

$ (window). Load (function () {
...

});

Summary comparison:

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.