After the browser finishes loading the DOM, events are added to the DOM elements through JavaScript, and in JavaScript, the Window.onload () method is usually used.
In jquery, the $ (document). Ready () method is used. Here are the differences between the two.
|
Window.onload () |
$ (document). Ready () |
Timing of execution |
Executes after all elements of the page (including pictures, reference files) are loaded. |
All HTML DOM,CSS DOM structures in the page are loaded and then executed, and other pictures may not be finished loading. If you want all content (including pictures, etc.) of the page to load, then register the event, using $ (window). Load (function); Equivalent to Window.onload () |
Number of writes |
Can not write multiple at the same time, the following will cover the front. Ex Window.onload=function () {alert ("A");} Window.onload=function () {alert ("B");} The result will be "B." If you want the sequential execution of alert ("A") and alert ("B") to be written as Window.onload=function () { Alert ("A"); Alert ("B"); } |
Can write multiple |
Shorthand |
No |
$ (document). Ready (function () { To do; }); can be written $ (). Ready (function () {//$ () with no parameters default is document
To do; }); $ (function () { To do; }); |