Differences between jquery $ (document). ready () and window. onload

Source: Internet
Author: User

1. execution time 

Window. onload can only be executed after all elements including images in the page are loaded.
$ (Document). ready () is executed after the DOM structure is drawn. You do not have to wait until the loading is complete.

2. different numbers of codes

Window. onload cannot be written at the same time. If there are multiple window. onload methods, only one
$ (Document). ready () can be compiled at the same time, and can all be executed.

3. Simplified writing

Window. onload is not simplified
$ (Document). ready (function () {}) can be abbreviated to $ (function (){});


Take a browser to load documents as an example. After the page is loaded, the browser adds an event to the DOM element through Javascript. In general Javascript code, the window. onload method is usually used, while in Jquery, the $ (document). ready () method is used. The $ (document). ready () method is the most important function in the event module and can greatly speed up Web applications.

Window. load $ (document). ready ()
The execution time must wait until all the content on the webpage is loaded (including images) before all DOM structures in the webpage can be executed after being drawn. The content associated with DOM elements is not loaded.
You cannot write multiple
The following code cannot be correctly executed:
Window. onload = function (){
Alert ("text1 ");
};
Window. onload = function (){
Alert ("text2 ");
};
Only the second result can be output and multiple results can be written at the same time.
Run the following code correctly:
$ (Document). ready (function (){
Alert ("Hello World ");
});
$ (Document). ready (function (){
Alert ("Hello again ");
});
Results are output twice.
Simplified syntax $ (function (){
// Do something
});

In addition, pay attention to the fact that in $ (document ). the event registered in the ready () method will be executed as long as the DOM is ready. Therefore, the associated file of the element may not be downloaded. For example, html related to images has been downloaded and parsed as DOM trees, but it is very likely that the images have not been loaded yet, therefore, the attributes such as the height and width of the sample slice are not necessarily valid at this time. To solve this problem, you can use the load () method, another method for page loading in Jquery. The Load () method binds a handler to the onload event of the element. If the processing function is bound to a window object, it is triggered after all the content (including the window, frame, object, and image) is loaded. If the processing function is bound to an element, it is triggered after the element content is loaded.
The Jquery code is as follows:
$ (Window). load (function (){
// Write code
}); Equivalent to the following code in JavaScript
Window. onload = function (){
// Write code
}

------------------------------

Recently, when I changed a page embedded in the frame, jquery was used for the effect, and the page itself was bound with the onload event. After the change, the test in Firefox is normal and smooth. in IE, it takes more than a dozen seconds for jquery to show up, and daylily is getting cooler.

At first, we thought it was a conflict with the onload loading method. $ (Document) is widely used on the Internet ). ready () is executed after the DOM parsing of the page is complete, while the onload event is executed after all resources are ready, that is, $ (document ). ready () is executed before onload, especially when the page image is large, the time difference may be greater. However, this page clearly shows that the images have been displayed for more than a dozen seconds, and jquery's effect is not displayed yet.

Try to delete the onload loading method. The result is the same. It seems that there is no need to bind the original onload event to $ (document). ready () for writing. What makes Firefox normal and IE can do that? After debugging, we found that the original onload method bound to IE exceeded $ (document ). ready (), while Firefox executes $ (document) first ). ready (), and then execute the original onload method. This is not exactly the same as the online statement.

Go through the jquery source code and see how $ (document). ready () is implemented:

Copy the Code as follows:
If (jQuery. browser. msie & window = top) (function (){
If (jQuery. isReady) return;
Try {
Document.doc umentElement. doScroll ("left ");
} Catch (error ){
SetTimeout (arguments. callee, 0 );
Return;
}
// And execute any waiting functions
JQuery. ready ();
})();
JQuery. event. add (window, "load", jQuery. ready );

The result is clear. IE runs $ (document) first only when the page is not embedded with frame, like Firefox ). ready (), and then execute the original onload method. For pages embedded in the frame, it is only bound to the load event for execution. Therefore, it is only after the execution of the original onload binding method. The page shows that there is an inaccessible resource in the test environment, and the latency of over 10 seconds is the time difference it zoomed in.

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.