Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Script placement is crucial for performance. Scripts block HTML parsing by default, so placing them correctly ensures your page loads fast and works properly.
defer in <head>. This prevents blocking page rendering.Visualizing where scripts can go
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Script Placement Options</title>
</head>
<body>
<div class="placements">
<div class="place-card head">
<h3><head></h3>
<p>With defer/async</p>
<code>Non-blocking</code>
</div>
<div class="arrow">→</div>
<div class="place-card body-start">
<h3>Body Start</h3>
<p>Rare use case</p>
<code>Blocks rendering</code>
</div>
<div class="arrow">→</div>
<div class="place-card body-end">
<h3>Body End ✓</h3>
<p>Best practice</p>
<code>HTML ready</code>
</div>
</div>
</body>
</html>Loading preview...
Scripts in head with defer attribute
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Script in Head</title>
<!-- Script in head with defer -->
</head>
<body>
<h1>Script Placement</h1>
<p id="msg">Loading...</p>
</body>
</html>Loading preview...
defer or async, scripts in <head> block HTML parsing. Always use defer for best results.Scripts after all HTML content
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Script at Body End</title>
</head>
<body>
<h1>Best Practice</h1>
<p id="result">Waiting...</p>
<!-- Script at end of body -->
</body>
</html>Loading preview...
✓ Advantage: HTML is fully loaded before script runs, no need for defer or DOMContentLoaded.
Different loading strategies
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Async vs Defer</title>
<!-- Async: download in parallel, execute ASAP -->
<!-- Defer: download in parallel, execute after HTML -->
</head>
<body>
<h1>Async vs Defer</h1>
<p>Check console for execution order</p>
<div class="info">
<strong>Async:</strong> Executes immediately when downloaded<br>
<strong>Defer:</strong> Waits until HTML is fully parsed
</div>
</body>
</html>Loading preview...
When: Simple projects, guaranteed HTML ready
✓ No attributes needed
⚠ Script discovery delayed
When: Modern best practice
✓ Early download, ordered execution
⚠ Requires defer attribute
When: Independent scripts (analytics)
✓ Fastest execution
⚠ Order not guaranteed
When: ES6 modules
✓ Auto defer, strict mode
⚠ Older browsers need polyfill