If you read the jQuery documentation for ready, you should take note of the line In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed.
My takeaway from this line is that this does not necessarily mean the page will display. So even though you do not have any css
code, that doesn't mean your document will display.
Here is a link to a StackOverflow question that does a good job explaining this: window.onload vs $(document).ready()
Here is a link to a page that describes the entire page rendering process: http://friendlybit.com/css/rendering-a-web-page-step-by-step/
If you take note of step 18 in the above link, after all the HTML & CSS is loaded, and after the Javascript is executed, then the page is actually printed. The following StackOverflow question contains good links to further illustrate this point: Describe the page rendering process in a browser?
Further clarification (in response to update 3):
Here's my summary for the friendlybit article:
- First you build up the HTML code itself. In this case, it would be your header elements.
- Then CSS is parsed.
- Then, because you called
document.ready()
, the Javascript is executed inside.
- In the case that your Javascript completes, then you finally have everything set-up all nicely, and you can proceed to actually render the page (another way to look at it is "paint" the page).
Here is a JSFiddle that shows this at work (you will need to open up console to see it work): http://jsfiddle.net/aLbmB/
The JSFiddle shows you that the two header elements are parsed before the Javascript is executed. If you change the code in the for-loop to the following:
$("#header2").text($("#header2).text() + "----");
i--;
You will see that the text never renders, which hopefully goes to show that the Javascript is being executed, and as a result, you never actually get to the page rendering part. Another good example is when you are pulling data from a database via jQuery and are waiting for it to all load in. The larger the data set, the longer it takes for the page to load (even though the HTML code is already there).
The way I think about this logically is that you first get all your values set-up nicely before you execute any Javascript on them. Then you execute the Javascript, and once all that is set in stone, you then allow your user to view your finished product. Of course, this is not the case when you don't use document.ready()
, but I guess the tradeoff there is speed vs. predictability (in execution).
Extra Note (courtesy of lazertyuiopl):
A good point was made that "you can trigger a paint of already parsed elements using alert()"
. If this is done, then it will appear as if each element is being painted one at a time, top to bottom. I would recommend checking this JSFiddle out: http://jsfiddle.net/K9kG8/7/
Run it again after it's loaded and you'll see what I believe Yugal Jindle expects.
Added aside: I think this question isn't a duplicate, but more of a question clarifiable through another question's answer. In other words, a specific example.