This article mainly introduces windows. onload () and $ (document ). ready () difference after the browser loads the DOM, it adds events to the DOM element through javascript. In javascript, window is usually used. onload () method.
In jquery, the $ (document). ready () method is used. The following describes the differences between the two.
|
Window. onload () |
$ (Document). ready () |
Execution time |
After all the elements (including images and referenced files) on the page are loaded, execute. |
All html dom and css dom structures on the page are executed after they are loaded. Other images may not be loaded. If you want to load all the content (including images) on the webpage and register the event, use $ (window). load (function ); Equivalent to window. onload () |
Write count |
You cannot write multiple data records at the same time, and the subsequent data will overwrite the previous one. Ex: Window. onload = function () {alert ("");} Window. onload = function () {alert ("B ");} The result is "B" If you want to execute alert ("A") and alert ("B") in sequence, you must write Window. onload = function (){ Alert (""); Alert ("B "); } |
You can write multiple |
Shorthand |
None |
$ (Document). ready (function (){ // To do; }); Can be written $ (). Ready (function () {// $ () without parameters, the default value is document.
// To do; }); Or $ (Function (){ // To do; }); |