Lesson 2 · Unit 01
Finding the rest
How the parser discovers files, what blocks it, and defer against async
What you practise
- The parser discovers stylesheets, scripts, images and fonts
- What blocks the parser
asyncanddefer- The preload scanner
Try a question from this lesson
Pick one
The script sits in the middle of the page. What does it log?
<p>A</p>
<script>
const n =
document.querySelectorAll("p").length;
console.log(n);
</script>
<p>B</p>
<p>C</p>Hint
The parser runs a script the moment it reaches it. What exists at that moment?
Correct answer
1
Why
The parser stops at the script tag and runs it right there. Only the first paragraph has been built, so it logs 1. B and C do not exist yet: the page grows as parsing goes on.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
The parser finds the rest
The HTML parser reads from top to bottom. When it meets a tag that names another file, the browser queues a request for it and, usually, the parser carries on.
Images never make it wait. A plain <script> does.
Concept
defer and async
Two attributes let the parser keep going while a script downloads.
defer: run after the whole document is parsed, in document order.
async: run as soon as the file arrives, whatever the parser is doing.
plain parse | fetch, run | parse
defer parse ............... | run
async parse ... | run | ... parseConcept
The preload scanner
While the parser is stuck on a script, a second, simpler scanner reads ahead in the raw HTML. It looks only for URLs to fetch: images, scripts, stylesheets.
So downloads start early, before that part of the page is built. It can only find what is written in the HTML itself.
9 more exercises in the app
- 2× type the answer
- 4× multiple choice
- 1× true or false
- 1× fill the blanks
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.