In jQuery, the ready function and window. onload are executed first. window. onload and ready
A. 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 (){...}, it is intended to execute function processing when the page is loaded, but these JS Code can only load all the content on the page (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.
The above is all about this article. For more information about jQuery syntax, see jQuery 1.10.3 online manual.