Window.onload and $ (document). The difference between ready (function () {})

Source: Internet
Author: User

Before the interview, I looked up and tidied up the Window.onload and $ (document). Ready (function () {}) difference, there is time to my blog today, because I am still shallow, if there is a wrong place, also please correct me.

Source: http://www.php100.com/html/program/jquery/2013/0905/5954.html

1, the difference in execution time: window.onload must wait until all the elements in the page (including the picture) are loaded into the browser before they can be executed. and $ (document). Ready (function () {}) is executed when the DOM structure is loaded.

2, write the number of different: Window.onload can not write multiple at the same time, if there are multiple window.onload, then only the last one will be executed, it will cover the front. $ (document). Ready (function () {}) is different, it can be written more than one, and each one will execute.

3, Shorthand method: Window.onload No Shorthand method, $ (document). Ready (function () {}) can be abbreviated to $ (function () {}).

Additionally: Because the event is registered within the (document). Ready () method, as long as the DOM is in place, it is possible that the element's associated file is not downloaded, such as the HTML download for the picture, and has been parsed into the DOM tree, but it is likely 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 an element, it is triggered when the content of the element is loaded. such as: $ (window). Load (function () {}) =====window.onload = function () {} ...

Window.onload and $ (document). The difference between ready (function () {})

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.