Chapter 03 • Lesson 3.9

Adding Scripts with <script>

Mastering the execution timeline: parser-blocking scripts, defer vs async, ES Modules, and <noscript> fallbacks.

🎯 Learning Objectives

📖 Mental Model: The Construction Crew & The Blueprints

Imagine a construction crew building a house brick-by-brick:

1. The Script Execution Timeline

By default, when an HTML parser encounters a <script src="..."> tag, it immediately pauses HTML parsing, downloads the script over the network, executes it, and only then continues parsing the remaining HTML.

1. DEFAULT <script src="..."> (Parser Blocking) HTML Parsing ────▶ [ PAUSED / BLOCKED ] ──────────────▶ HTML Parsing Resumes ├── Fetch JS ──▶ Execute JS ──┤ 2. DEFERRED <script defer src="..."> (Ordered & Non-blocking - BEST PRACTICE) HTML Parsing ─────────────────────────────────────────▶ DOMContentLoaded ──▶ End └── Fetch JS in Parallel ─────────────────────────────▶ [ Execute in Order ] 3. ASYNCHRONOUS <script async src="..."> (Independent / Analytics) HTML Parsing ──────────▶ [ PAUSE ] ─────────▶ HTML Parsing Resumes ───────▶ End └── Fetch JS Parallel ─▶ [ Exec Now ] (Executes the microsecond download finishes) 4. ES MODULE <script type="module" src="..."> HTML Parsing ─────────────────────────────────────────▶ DOMContentLoaded ──▶ End └── Fetch Module Graph in Parallel ───────────────────▶ [ Execute in Order ]

2. Decision Matrix: Which Script Loading Method to Use?

Loading Method Parser Blocking? Execution Timing Order Guaranteed? Best Use Case
Default <script> 🔴 Yes (Blocks) Immediately when encountered Yes Legacy code only. Avoid in modern apps.
<script defer> 🟢 No (Parallel) After DOM parsing, before DOMContentLoaded 🟢 Yes (Document Order) App logic, UI components, libraries depending on other scripts.
<script async> 🟡 Pauses during execution Immediately when download finishes 🔴 No (Race condition) Independent analytics (Google Analytics, tracking pixels, ads).
<script type="module"> 🟢 No (Parallel) After DOM parsing (deferred by default) 🟢 Yes (Dependency tree) Modern JavaScript (ES6+ import / export).

3. The <noscript> Graceful Fallback

The <noscript> element defines HTML to be inserted if a script type is unsupported or if scripting is disabled in the user's browser.

<noscript>
  <div class="alert">
    ⚠️ JavaScript is disabled in your browser. Please enable JavaScript for the full interactive experience.
  </div>
</noscript>

4. Interactive Live Playground: Script Execution Sandbox

Observe how script manipulation modifies the DOM dynamically:

SYS: ACTIVE
HULL: 98%
CORE: STABLE
NET: ONLINE
HTML STARSHIP CODE TERMINAL index.html
LIVE RENDER & DIAGNOSTICS CORE TEMP: 45°C
INSPECTING DOM: VALID
TAGS: SCANNING...

🏋️ Hands-On Exercise: DOM-Safe Script Interaction

  1. Look at the starter code below. Notice that scripts are written to interact with DOM elements.
  2. Add an interactive theme toggle script that toggles a dark-mode class on a container card.
  3. Add a <noscript> warning block informing users if JavaScript is disabled.
  4. Test the button to verify the theme toggles smoothly.
SYS: ACTIVE
HULL: 98%
CORE: STABLE
NET: ONLINE
HTML STARSHIP CODE TERMINAL exercise.html
LIVE RENDER & DIAGNOSTICS CORE TEMP: 45°C
INSPECTING DOM: VALID
TAGS: SCANNING...

⚠️ Common Pitfalls

💡 Pro Tips

📌 Key Takeaways

⭐ LEARN: HTML 🌟 ⚔️ QUIZ BATTLE ARENA // ACTIVE
3x
STREAK!
BONUS ACTIVE
COMBO
? Question 1 / 3

Which script attribute downloads scripts in parallel and guarantees execution in document order after HTML parsing?

Question 1 / 3 Topic: HTML Fundamentals
14:28 REMAINING
XP REWARD
+250 XP
3x
STREAK!
BONUS ACTIVE
COMBO
? Question 2 / 3

Why is async NOT recommended for scripts that depend on each other (e.g. plugins)?

Question 2 / 3 Topic: HTML Fundamentals
14:28 REMAINING
XP REWARD
+250 XP
3x
STREAK!
BONUS ACTIVE
COMBO
? Question 3 / 3

When does the browser render content inside a <noscript> element?

Question 3 / 3 Topic: HTML Fundamentals
14:28 REMAINING
XP REWARD
+250 XP