Draw an analogy to compare the process of loading a web page to a chef with a recipe:
Firstly, the chef (browser) reads the entire recipe (loads an HTML document) to make sure that he understands the steps (HTML code) and that he knows what ingredients (images), dishes (style sheets) and appliances (external scripts) that he will have to have in his kitchen (browser cache).
As the chef continues to read, he sends his assistant to the pantry to get ingredients, utensils and appliances (download other files from the server). When he finishes reading the recipe ( $(document).ready() ), he begins to follow the steps (displays the page), but sometimes he reaches a step before his assistant returns with the necessary materials to complete this step. He is quite experienced, so he is still able to complete the later steps while he waits. (The analogy breaks down a bit here, but basically: the browser displays the page in the same way that it can be based on HTML when you see the page load, and then the fonts or layout change after a couple of seconds because finally got the stylesheet ... just imagine that this chef can somehow add eggs to a cake that is already in the oven.)
It is only after the assistant chef brought everything that is indicated in the recipe back to the kitchen (the browser downloaded all the contents) that the chef can put the finished meal on a plate and decorate it (run the onload code).
The onload event is a standard event in the DOM, and the finished event is specific to jQuery.
The DOM is a document object model, the basic component of normal JavaScript. This means that all modern web browsers already know what onload means.
jQuery is a widely used JavaScript library. For your script to use $(document).ready() correctly, you will have to reference a copy of the jQuery library. Browsers do not know what $(document).ready() means without jQuery.
Dan hederson
source share