JavaScript's Window.onload () method and jquery's $ (document). The comparison of Ready ()

Source: Internet
Author: User

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

1. Execution time

Window.onload must wait until all the elements of the page that include the picture have been loaded before they can be executed.

$ (document). Ready () is executed when the DOM structure is drawn and does not have to wait for the load to complete.

2. Different writing numbers

Window.onload cannot write multiple at the same time, if there are multiple window.onload methods, only one will be executed and the previous will be overwritten later.
$ (document). Ready () can be written multiple at the same time and can be executed

3. Simplified notation

Window.onload No simplified notation
$ (document). Ready (function () {}) can be simply written as $ (function () {});

window.onload () method and $ (document) comparison of Ready ()
Window.onload $ (document). Ready ()
Timing of execution You must wait for all content in the Web page to finish loading (including pictures) to perform After all the DOM structures in the Web page are drawn, the content that can be associated with the DOM element is not loaded
Number of writes

Cannot write multiple

The following code does not execute correctly:

 window.onload =   = function   
The

result outputs only the second output: ' Test2 ';

  can write multiple

At the same time the following code executes correctly:

 $ (document). Ready (function  Span style= "color: #000000;" > () {alert ("Hello World");}); $ (document). Ready ( function   () {alert (" Hello again "); });  

Results are output twice, first output hello World , and then output

Simplified notation No
$ (document). Ready () {//}; can be simply written as: $ (function(   ) {// 

Note Point 1:

Because the event is registered within the (document). Ready () method, as long as the DOM is in place, it is possible that the associated file for the element is not finished downloading at this time. For example, the HTML associated with the picture has already been downloaded and parsed into a DOM tree, but it is possible that the picture has not been loaded yet, so attributes such as the height and width of the case are not necessarily valid at this time. To solve this problem, you can use another method in JQuery about page loading---the load () method. The load () method binds a handler function in the OnLoad event of the element. If the handler is bound to a Window object, it fires after all content (including Windows, frames, objects, images, and so on) is loaded, and if the handler is bound to the element, it is triggered after the element's contents have been loaded.
The JQuery code is as follows:

$ (window). Load (function  () {       //});

Equivalent to the following code in JavaScript

function () {     //  write code }

Note Point 2:

In general, JavaScript is used, and we are all using the jquery pattern, that is, most of the time, the first line reads:

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

This time, do not have to wait for all the JS and pictures 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 what other aspects have not loaded well, this time, click on some buttons, Will cause an unexpected situation, this time, you need to use:

$ (window). Load (function() {$ ("#btn-upload"). Click (function() {   //  For example , Uploadphotos ();} );

JavaScript's Window.onload () method and jquery's $ (document). The comparison of Ready ()

Related Article

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.