Script loading and image priorities
Script attributes and image loading choices affect when work happens and how quickly important content becomes visible.
✓ What you will learn in this topic
- Explain the purpose of Script loading and image priorities in a real HTML document.
- Choose and apply the elements shown in both examples.
- Recognize the specific mistake described in this lesson.
Concept explanation
A deferred classic script downloads in parallel and executes after HTML parsing, preserving document order among deferred scripts. Async scripts execute when ready and do not guarantee order, so use them for independent work. Module scripts defer by default. Loading=\"lazy\" defers offscreen images, but the main image visible at first paint should not be delayed. Use performance measurement rather than adding attributes mechanically.
Syntax
Choose defer for ordered scripts that need the parsed document, async for independent scripts, and lazy loading for noncritical offscreen media.
Example 1: a small page
The script waits for parsing and the below-fold image can wait until it approaches the viewport.
<script src=\"/assets/site.js\" defer></script>
<img src=\"/images/story.jpg\" alt=\"A reader at the library window\" width=\"800\" height=\"520\" loading=\"lazy\">Expected result: The document parses before the script executes, and the noncritical image is loaded near its scroll position.
Example 2: a practical page
Analytics is independent; navigation waits for parsing. The likely initial hero is not lazy-loaded.
<script src=\"/assets/analytics.js\" async></script>
<script src=\"/assets/navigation.js\" defer></script>
<img src=\"/images/hero.jpg\" alt=\"The new riverside learning centre\" width=\"1440\" height=\"900\">Expected result: The analytics file may run whenever ready, navigation runs after parsing, and the hero can load promptly.
Common mistakes
Do not preload resources speculatively; an incorrect preload can compete with more important page content.
Best practices
Measure first, prioritize the actual critical resource, and use async or defer according to script dependencies.
Practice exercise
Review a page head with a font, hero image, and two scripts. Decide which resource is truly critical and justify any preload, preconnect, async, or defer hint.
Lesson summary
- Match script loading to dependencies and avoid delaying the image people see first.
- Measure first, prioritize the actual critical resource, and use async or defer according to script dependencies.
Knowledge check
When is async appropriate for a script?
Show the explanation
When it is independent and can safely execute as soon as its download completes, without relying on execution order.
Practical example
html<script src=\"/assets/site.js\" defer></script>
<img src=\"/images/story.jpg\" alt=\"A reader at the library window\" width=\"800\" height=\"520\" loading=\"lazy\">
Expected result:
The document parses before the script executes, and the noncritical image is loaded near its scroll position.
Hands-on practice task
Review a page head with a font, hero image, and two scripts. Decide which resource is truly critical and justify any preload, preconnect, async, or defer hint.
In this section
Other lessons in HTML Performance
Broaden your knowledge