CHAPTER 56 · HTML
Templates and browser features: where HTML needs JavaScript
Reuse a small piece of markup and remember one harmless preference. Understand which jobs belong to HTML and which need browser features and JavaScript.
Download example files (ZIP)A familiar starting point
Read the template syntax
The first script, one step at a time
What is a browser API?
Local storage and session storage
Read the preference example
Other browser features: know their jobs first
Try a small change
Follow the idea
- 1Find the template
- 2Clone its content
- 3Fill the copy with text
- 4Append the copy
The template remains a reusable draft.
Complete program: template-card.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Build a Study Card</title>
</head>
<body>
<h1>Make one card from a template</h1>
<main>
<p>The template holds a draft. The button copies it into the page once.</p>
<button type="button" id="add-card">Add study card</button>
<div id="cards"></div>
<p id="card-status" role="status">No card added yet.</p>
<template id="card-template"
><article>
<h2>Draft heading</h2>
<p>Draft note</p>
</article></template
>
<script>
const template = document.getElementById('card-template');
const button = document.getElementById('add-card');
button.addEventListener('click', () => {
const copy = template.content.cloneNode(true);
copy.querySelector('h2').textContent = 'Link practice';
copy.querySelector('p').textContent = 'Check where one relative link leads.';
document.getElementById('cards').append(copy);
document.getElementById('card-status').textContent = 'Study card added.';
button.disabled = true;
});
</script>
</main>
</body>
</html>This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here. JavaScript does not run in this preview. Save the complete example and any named script file together to test its actions or canvas drawing in your browser.
Complete program: stored-topic.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Remember a Practice Topic</title>
</head>
<body>
<h1>Save one fictional preference</h1>
<main>
<p>Use this over local HTTP. Saving is for this browser and origin, not a student account.</p>
<p>
<label for="saved-topic">Practice topic</label>
<select id="saved-topic" name="topic">
<option value="links">Links</option>
<option value="forms">Forms</option>
</select>
</p>
<button type="button" id="save-topic">Save topic</button>
<button type="button" id="forget-topic">Forget topic</button>
<p id="storage-status" role="status">No saved preference loaded.</p>
<script>
const key = 'resume-day-html-practice-topic';
const select = document.getElementById('saved-topic');
const status = document.getElementById('storage-status');
try {
const saved = localStorage.getItem(key);
if (saved === 'links' || saved === 'forms') {
select.value = saved;
status.textContent = 'Loaded topic: ' + saved;
}
} catch {
status.textContent = 'Storage is unavailable. You can still choose a topic.';
}
document.getElementById('save-topic').addEventListener('click', () => {
try {
localStorage.setItem(key, select.value);
status.textContent = 'Saved topic: ' + select.value;
} catch {
status.textContent = 'Could not save. Browser storage may be blocked or full.';
}
});
document.getElementById('forget-topic').addEventListener('click', () => {
try {
localStorage.removeItem(key);
select.value = 'links';
status.textContent = 'This practice preference was removed.';
} catch {
status.textContent = 'Could not remove the practice preference.';
}
});
</script>
</main>
</body>
</html>This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here. JavaScript does not run in this preview. Save the complete example and any named script file together to test its actions or canvas drawing in your browser.
Problem and solution
Common mistakes and useful tips
A template does not display itself. Use textContent for plain text instead of inserting supplied text as markup. Avoid duplicate ids in copied cards. Remove one owned key rather than calling localStorage.clear(). Keep a useful page when storage is blocked.
Practice questions
Does a template appear automatically? Why clone instead of moving the draft? What does localStorage.getItem return for a missing key? Save Forms, reload, forget, then reload. What should be selected? Does saving here create a student account? Which feature would need a real server event stream?
Check your practice answers
No. Its ordinary contents wait until code inserts a copy into the displayed document. A clone leaves the original template contents available for later copies. Null. Our example leaves the default topic selected. Forms after saving and reloading; Links after forgetting and reloading, if storage works. No. It saves one browser preference for this origin. Server-sent events; an HTML heading alone cannot produce live server updates.
Interview questions
What is the difference between HTML and a browser API?
Why handle storage errors?
Can a web worker directly change a heading?
Is local storage suitable for passwords?
Quick revision
Template contents are a draft; clone before reuse. Browser APIs need JavaScript and feature-specific handling. Storage belongs to an origin and stores strings. Handle failure and remove only your own key.