The difference between jquery's ready () and window. onload () Is that jqueryonload
$ (Document) in Jquery is often used for web development ). window in ready () and JavaScript. the onload method, both of which must be loaded after the page is loaded, but the two are quite different. I recently encountered such a problem. I have searched many articles and made a summary.
To put it simply, we need to use the following table:
Ready () of Jquery and load () of Javascrpit ()
|
Window. onload () |
$ (Document). ready () |
Loading time |
You must wait until all webpages are loaded (including images) and then execute the JS Code. |
The javascript code can be executed only after the DOM structure in the webpage is loaded. |
Number of executions |
It can only be executed once. If the second execution is performed, the first execution will be overwritten. |
It can be executed multiple times, and N times won't be overwritten at the previous time |
Example |
The following code cannot be correctly executed: window.onload = function() { alert(“text1”);}; window.onload = function() { alert(“text2”);}; Only the second result is output. |
Run the following code correctly: $(document).ready(function(){alert(“Hello”)});
$(document).ready(function(){alert(“Hello”)}); Results are output twice. |
Simplified Solution |
None |
$ (Function (){}) |
I think $ (document) ready of jQuery is a bit like windowonload. It is executed when loading html. I don't know what it means, right?
1.
Dom Ready can directly perform dom operations after the dom is loaded. For example, if the tag of an image is complete, you do not need to wait until the image is loaded, you can set attributes or styles of the image;
2.
Dom Load directly performs dom operations after the entire document file (including loading images and other information) is loaded, for example, an image can be set to a wide or high attribute or style after the icon is loaded;
What is the difference between jquery's ready () method and <body onload = "">?
Jquery ready can be written several times, each of which is executed
Onload can only write one, you write several, but also only execute one, as if to execute the last