Differences between jquery $ (document). ready () and window. onload _ javascript skills

Source: Internet
Author: User
This article mainly introduces jquery $ (document ). ready () and window. onload differences, This article summarizes the execution time, the number of different writes, simplified writing and other different places, need friends can refer to Jquery $ (document ). ready () is similar to Windows in traditional JavaScript. onload method, but with window. the onload method is different.

1. execution time
Window. onload can only be executed after all elements including images in the page are loaded.
$ (Document). ready () is executed after the DOM structure is drawn. You do not have to wait until the loading is complete.
2. different numbers of codes
Window. onload cannot be written at the same time. If there are multiple window. onload methods, only one
$ (Document). ready () can be compiled at the same time, and can all be executed.
3. Simplified writing
Window. onload is not simplified
$ (Document). ready (function () {}) can be abbreviated to $ (function (){});

In my previous development, javascript is generally used. I use the jquery mode, that is, most of the time, the first line is written as follows:

The Code is as follows:


$ (Document). ready (function (){
...
});

At this time, you may not have to wait until all js and image loads are completed, but sometimes you have to wait for all

When all elements are loaded, some methods can be executed. For example, some images or other aspects have not been loaded. At this time, clicking some buttons will lead to unexpected situations, at this time

Use:

The Code is as follows:


$ (Window). load (function (){
...

});

Summary and 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.