Home >Web Front-end >JS Tutorial >A table tells you the difference between windows.onload() and $(document).ready()_jquery
After the browser loads the DOM, it will add events to the DOM elements through javascript. In javascript, the window.onload() method is usually used.
In jquery, use the $(document).ready() method. Let’s introduce the difference between the two.
window.onload() | $(document).ready() | |||||||||||||
Execution Timing | Executed after all elements of the page (including images and referenced files) are loaded. |
|
||||||||||||
Write number | You cannot write multiple ones at the same time, the later ones will overwrite the previous ones. ex: window.onload=function(){ alert("A"); } window.onload=function(){ alert("B"); } The result will be "B" If you want to execute alert("A") and alert("B") sequentially, you need to write window.onload=function(){ alert("A"); alert("B"); } | You can write multiple ones at the same time | ||||||||||||
Abbreviation | None | $(document).ready(function(){ //to do; }); can be written as $().ready(function(){ //$() without parameters defaults to document //to do; }); or $(function(){ //to do; }); |