The difference between jquery's ready () and window. onload () Is that jqueryonload

Source: Internet
Author: User

The difference between jquery's ready () and window. onload () Is that jqueryonload

$ (Document) in Jquery is often used for web development ). window in ready () and JavaScript. the onload method, both of which must be loaded after the page is loaded, but the two are quite different. I recently encountered such a problem. I have searched many articles and made a summary.

To put it simply, we need to use the following table:

Ready () of Jquery and load () of Javascrpit ()
  Window. onload () $ (Document). ready ()
Loading time

You must wait until all webpages are loaded (including images) and then execute the JS Code.

The javascript code can be executed only after the DOM structure in the webpage is loaded.

Number of executions

It can only be executed once. If the second execution is performed, the first execution will be overwritten.

It can be executed multiple times, and N times won't be overwritten at the previous time

Example

The following code cannot be correctly executed:

window.onload = function()  { alert(“text1”);}; window.onload = function()  { alert(“text2”);};  

Only the second result is output.

Run the following code correctly:

$(document).ready(function(){alert(“Hello”)}); 

$(document).ready(function(){alert(“Hello”)});

Results are output twice.

Simplified Solution None $ (Function (){})

I think $ (document) ready of jQuery is a bit like windowonload. It is executed when loading html. I don't know what it means, right?

1.

Dom Ready can directly perform dom operations after the dom is loaded. For example, if the tag of an image is complete, you do not need to wait until the image is loaded, you can set attributes or styles of the image;
2.

Dom Load directly performs dom operations after the entire document file (including loading images and other information) is loaded, for example, an image can be set to a wide or high attribute or style after the icon is loaded;

What is the difference between jquery's ready () method and <body onload = "">?

Jquery ready can be written several times, each of which is executed
Onload can only write one, you write several, but also only execute one, as if to execute the last

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.