ready function in jQuery and who executes window.onload first

  • 2021-06-29 09:41:05
  • OfStack

A.About $(document).ready():

What exactly does $(document). ready() in jquery do?Can we use window.onload = function(){...}?

Here, we want to clarify the difference between the two.

We use window.onload = function (){...} in order to execute the processing in function when the page is loaded, but these JS codes will only execute when the entire content on the page is loaded (including the banner advertisement in the header, all pictures).The reason you put window.onload on the top is that when you first ran codes, HTML'document'was not loaded yet.

Whereas $(document). ready() does not need to be loaded so "completely". $(document). ready() is executed after the DOM structure has been loaded, while window.onload must be executed after all files have been loaded. Note the difference between DOM and all files.Therefore, ready must have happened before onload, and onload was delayed by loading large or many pictures on the page. ready with jquery can alleviate this problem.

It should be kept in mind that ready of jquery refers to the function specified to be executed after the DOM model of the page has been loaded.The reason why $(document).ready() is often used instead of window.onload is because it executes after the dom model is loaded, and window.onload is executed 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, which refers to the execution of a specified function after a page has been loaded.

Make a comparison:

window .onload =function (){alert ("welcome");}
 function (){
  alert ("thanks for visiting!");

After running, you will find that $(document). ready() executes first.

Related articles: