About $ (document). Ready ():
$ (Document). Ready () in jquery, what is the role of $ (document). Ready? Can I use window. onload = function () {...} to implement it?
Here, we need to clarify the differences between the two.
We use window. onload = function () {...} to execute function processing when the page is loaded, but these JSCodeIt is executed only when all content on the page is loaded (including banner advertisements in the header and all images. The reason for putting window. onload at the top is that when you run codes for the first time, the HTML 'document' has not been loaded yet.
$ (Document). Ready () does not need to be loaded completely ". $ (Document ). ready () is executed after the DOM structure is loaded, while window. onload is executed after all files are loaded. Note the difference: one is that the Dom is loaded, and the other is that all files are loaded. Therefore, ready must occur before onload. Loading Large or many images on the page will delay onload execution. Using jquery ready can alleviate this problem.
We should note that jquery's ready refers to executing the specified function after the DOM model of the page is loaded. $ (Document) is often used ). ready () to replace window. onload is because it is executed after the DOM model is loaded, while window. onload is executed only after the DOM element is fully loaded.
B. About document. onload and window. onload:
There is no essential difference between document. onload and window. onload. They indicate that the specified function is executed after the page is loaded.
Make a comparison:
Window. onload = function () {alert ("welcome ");}
$ (Document). Ready (
Function (){
Alert ("thanks for visiting! ");
}
);
After running, you will find that $ (document). Ready () is executed first.