I have done some web based projects, but I don't think too much about the load and execution sequence of an ordinary web page. But now I need to know detail. It's hard to find answers from Google or SO, so I created this question.
A sample page is like this:
So here are my questions:
- How does this page load?
- What is the sequence of the loading?
- When is the JS code executed? (inline and external)
- When is the CSS executed (applied)?
- When does $(document).ready get executed?
- Will abc.jpg be downloaded? Or does it just download kkk.png?
I have the following understanding:
- The browser loads the html (DOM) at first.
- The browser starts to load the external resources from top to bottom, line by line.
- If a
<script>is met, the loading will be blocked and wait until the JS file is loaded and executed and then continue.
- Other resources (CSS/images) are loaded in parallel and executed if needed (like CSS).
Or is it like this:
The browser parses the html (DOM) and gets the external resources in an array or stack-like structure. After the html is loaded, the browser starts to load the external resources in the structure in parallel and execute, until all resources are loaded. Then the DOM will be changed corresponding to the user's behaviors depending on the JS.
Can anyone give a detailed explanation about what happens when you've got the response of a html page? Does this vary in different browsers? Any reference about this question?
I did an experiment in Firefox with Firebug. And it shows as the following image: