Home >Web Front-end >JS Tutorial >Summary of the use of $(document).ready() in jquery_jquery
window.onload = function(){ alert("welcome"); }
The purpose of this writing method is to automatically execute the defined js code (function) after the page is loaded.
$(document).ready(function(){.... }) This function is used to replace window.onload in the page;
document.ready() is similar to the traditional method
. The difference is that the onload() method occurs only after the page is loaded, including DOM elements and other pages. The loading of elements (such as images), therefore, using the document.ready() method performs faster than the onload() method.Javascript can only perform certain operations on DOM elements after they have been defined. jQuery uses document.ready to ensure that the code to be executed is executed after the DOM element has been loaded.
For example:
Finally two points to note:
Make sure there is no registered function in the onload event of the
element, otherwise the $(document).ready() event may not be triggered. (I would like to say: