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

Source: Internet
Author: User

$ (document). Ready (...) and window.onload: events have a similar effect, but there are subtle differences in the time they are triggered.

1.window.onload:
The Window.onload event is triggered when a document is completely downloaded to the browser. This means that all the elements on the page are accessible to JavaScript, which is advantageous for writing functional code because there is no need to consider the order of loading.

2.$ (document). Ready ():
Event handlers registered through the $ (document). Ready () are called when the DOM is fully in place and can be used. While this also means that all elements are accessible to the script, missing does not mean that all associated files have already been downloaded. In other words, after the HTML download is complete and parsed into the DOM tree, the code can run. All elements or events that are included in the. Ready () will load immediately after the DOM finishes loading and before the page content is loaded. Using the $ (document). Ready (), you can let your event load or trigger before the window loads. All you write in this method is ready to load or trigger at the earliest moment. That is, once the DOM is registered in the browser, the code in the Ready () is executed. This allows the user to see the page element at the first glance, and the effect is ready to run.

Concise summed up as:
Window.onload: The document and all associated external files have all been downloaded.
$ (document). Ready (): The document has been downloaded, but all the associated external files are not necessarily all downloaded.

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

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.