The difference between JavaScript Tips:window.onload and $ (document). Ready ()

Source: Internet
Author: User

1, window.onload in $ (document). Ready () after execution

Cause: Both trigger or not, are based on the document.readystate attribute.

(1) Value of document.readystate: Uninitialized, loading, interactive, complete (these four values, sorted by time).

(2) When its value is interactive, the documentcontentloaded event is triggered, and Jquery.ready () is a function designed for the documentcontentloaded event. This executes the task queue defined by the Jquery.ready.

(3) When the value is complete, the Load event is triggered.

(4) In summary, Window.onload will be executed after $ (document). Ready ().

Original agreement: document .readyState

Returns " loading Document while the is loading," interactive "once it was finished parsing but still loading sub-resources, and" "Once it has loaded.

The readystatechange event fires on the Document object and this value changes.

2, window.onload multiple assignments will produce coverage, and $ (document). Ready () does not.

Reason:

(1) onload is a member method of the Window object and is a reference to an object of type function in heap memory. Because JavaScript does not have a function overloading mechanism, assigning a value to the onload multiple times causes the last reference to a function to be dismissed, instead referring to the reference to the current function, resulting in an overlay.

(2) $ (document). Ready () Implements a task object, passing in the function multiple times, adding a member to the task queue.

The difference between JavaScript Tips:window.onload and $ (document). Ready ()

Related Article

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.