Differences between onload and $ (document). Ready ()

Source: Internet
Author: User

Onload needs to be executed after all resources on the page are loaded, while ready is when the DOM document tree has been parsed, ready is faster than onload. For example, a large image is displayed on a page, which takes a long time to load. onload is executed only after the image is loaded, while ready does not have to wait until the image is loaded.

In $ (document ). when ready () is executed, the entire DOM document tree has been parsed, that is, each Dom element can be accessed (but the access to some attributes of some elements may not be accurate at this time, width and height of the slice ). $ (Function () {}) can be placed after it, possibly before ready.

 

 

 

1. It is onload, not onload.
2. No document. onload, only window. onload.
3. Window. onload = myfunction, rather than window. onload = myfunction (). If it is written as the latter, when it is executed here, it calculates the return value of myfunction () and passes it to window. onload. Only write the former, and direct the myfunction to window. onload (similar to the pointer variable in C)

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.