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

Source: Internet
Author: User

recent project to do a message alert function, after the user login is completed, from the lower right corner of a message module, similar to QQ pop-up ads, but in the browser, a little more. The beginning of the practice is in $ (document). Ready () is used in the jquery slidedown display message div, because the page contains six or seven blocks is an IFRAME, each iframe load time is relatively long, the content is more, Results The IFRAME is not loaded, the message div out, and the animation of a card, the effect is unbearable. and then a piece of article on the jquery official web(http://learn.jquery.com/about-jquery/how-jquery-works/)Find the $ (document) for jquery. Ready () is not when the page is all loaded and then executed, but the current page of the DOM loaded after the execution, so high efficiency, fast. But to wait until the page is all loaded, including the internal IFRAME is loaded and then executed, but also to use the Window.onload event, the following is said Window.onload and $ (document). The difference between ready (). The main is the execution time is different, above also said Window.onload must wait until the page includes the picture, the IFRAME and so on all elements to execute after loading, and the $ (document). Ready () is executed after the DOM structure is drawn, without waiting for the load to complete, In other words, the image may not have been downloaded and the contents of the IFRAME have not been loaded. the execution time of the two is different, the effect is also different, in most cases $ (document). Ready () works better, but it is important to note that because $ (document) is executed when the picture is not loaded. Attributes such as image size are not necessarily valid. But when all the pages are loaded and then get a picture of the size, and wasted time, how to do? Fortunately, jquery provides us with a better way to use the Load method to bind the specified element to the event at the completion of loading, such as $ ("img"). Load (function () {...});Another benefit of the load method is that if you simply write the Window.onload register event, only the last one will be executed, and if you want to register more than one, you need additional code to judge, while the load method using jquery can register multiple events, $ (window). Load ( ) and window.onload are equivalent, but the Load method can register multiple events.


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.