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

Source: Internet
Author: User

1. Execution Time

Window.onload must wait until all the elements of the page that include the picture have been loaded before they can be executed.  

$ (document). Ready () is executed when the DOM structure is drawn and does not have to wait for the load to complete.  

2. different writing numbers

         window.onload 

$ (document). Ready () can be written multiple at the same time and can be executed

3. simplified notation

Window.onload No simplified notation

$ (document). Ready (function () {}) can be abbreviated to $ (function () {});

Jquery ---load () method. Load () onload event binding a handler function. If the handler is bound to window () after loading, if the handler is bound to the element, it will be triggered when the content of the element is loaded.

The Jquery code is as follows:

$ (window). Load (function () {

writing code

}); equivalent to the following code in JavaScript

Window.onload = function () {

writing code

}

——————————————————————————————

framejqueryonload< Span style= "font-family: ' Microsoft yahei UI ';" > event. After the change, firefox test normal and smooth, ie > Wait a few minutes. jquery The effect only appeared, the Lily is cold.

initially thought to be in conflict with the method itself that onload loads. The popular view on the Internet is $ (document). Ready () is executed after the page DOM parsing is complete, and the onload event is executed after all resources are ready to complete, that is, $ (document The. Ready () is performed before onload, especially if the page picture is larger, the time difference may be greater. But my page is clear that the pictures are displayed for more than 10 seconds, but also see the effect of jquery out.

deleted theOnLoadLoad the method to try, the result is the same, it seems that there is no need to put the originalOnLoadEvent bindings also use the$ (document). Ready ()ie can you do that? Then debug, found ie onload$ (document). Ready () firefox$ (document). Ready () onload method. This and the online statement does not seem to be exactly the same ah, oh, a little meaning, seems to be closer to the truth.

Turn over the source of jquery to see the $ (document). How to do it:

Copy the code code as follows :

if (jQuery.browser.msie && window = = top) (function () {

if (Jquery.isready) return;  

try {

Document.documentElement.doScroll ("left");  

} catch (Error) {

SetTimeout (Arguments.callee, 0);  

Return  

and execute any waiting functions

Jquery.ready ();  

})();  

JQuery.event.add (window, "load", jquery.ready);

The results are clear,ieframefirefox$ (document). Ready () onload method. For embedded frameload The event is executed, so naturally in the original onload< Span style= "font-family: ' Microsoft yahei UI ';" > The bound method executes only after the turn. And this page is just in the test environment, there is an inaccessible resource, the delay of more than 10 seconds is its magnified time difference.

&NBSP;

turn from >>>  

jquery $ (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.