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 on the webpage are drawn, the content that can be associated with DOM elements is not loaded.
You cannot write multiple codes at the same time.
The following Code is incorrect. run:
window. onload = function () {
alert ("text1");
};
window. onload = function () {
alert ("text2");
};
only the second result can be output.
the following code is correctly executed:
$ (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.