BCA Student Stories & UpdatesLive
Client-Side SPA Routing & Browser History API (pushState & popstate)
Modern frontend frameworks like React, Next.js, and Vue power seamless Single Page Applications (SPAs). In an SPA, the browser never downloads a fresh HTML page when navigating between sections. Instead, client-side JavaScript intercepts navigation clicks, updates the URL using the HTML5 History API, and renders the corresponding view in 0ms.
Day 47 Step-by-Step Practical Flow
🎯1. What You Will Learn Today
Client-Side SPA Routing & Browser History API (pushState & popstate) — Comprehensive Practical Guide
Har concept ko visual mental models, code anatomy aur practical industry tips ke sath samjhein.
Modern frontend frameworks like React, Next.js, and Vue power seamless Single Page Applications (SPAs). In an SPA, the browser never downloads a fresh HTML page when navigating between sections. Instead, client-side JavaScript intercepts navigation clicks, updates the URL using the HTML5 History API, and renders the corresponding view in 0ms.
"Think of traditional websites like reading a stack of separate paper textbooks: turning a page requires closing one book, putting it on a shelf, and opening another book. An SPA is like an Amazon Kindle: the physical frame never changes, but tapping a button instantly swaps the text on screen."
👨🏫3. Teacher Demonstration Code
See how views are swapped instantaneously and how the browser Back/Forward buttons remain 100% functional without page reloads.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vanilla SPA Router</title>
<style>
body { font-family: system-ui, sans-serif; background: #060c18; color: #fff; padding: 24px; }
nav a { color: #94a3b8; text-decoration: none; font-weight: bold; margin-right: 12px; }
nav a.active { color: #38bdf8; }
</style>
</head>
<body>
<nav id="navBar">
<a href="#/home" class="active">🏠 Home</a>
<a href="#/about">👨💻 About</a>
</nav>
<div id="appView" style="margin-top:20px;"></div>
<script>
const routes = {
"#/home": "<h2>Welcome Home</h2><p>BCA Zero-lag practicals.</p>",
"#/about": "<h2>About Nextsem</h2><p>Authored by Er. Manoj Kumar.</p>"
};
function render() {
const hash = location.hash || "#/home";
document.getElementById("appView").innerHTML = routes[hash] || "<h2>404</h2>";
}
window.addEventListener("hashchange", render);
render();
</script>
</body>
</html>2. Hands-On Practical Lab: Follow Instructions in VS Code
Code like a professional software engineer! Follow the step-by-step instructions below in Visual Studio Code (VS Code) on your PC, test your output locally, and push to GitHub.
Day 47 Practical Task Test Studio
Call history.pushState({ page: "dashboard" }, "", "/dashboard") to update the URL client-side.
Add a window popstate event listener that logs the current history state object.
Inside an anchor click event listener, call e.preventDefault() before running router logic.
Spot the Bug: Broken Back Button in Client-Side State Router
Tap or click on the exact line below that contains the error:
Click any code linefunction navigate(url) { history.pushState(null, "", url); render(url);}// ❌ Bug: popstate is never listened to!Senior Developer Forensic Analysis:
pushState() informs the browser to push a history entry, but clicking the browser Back/Forward buttons does NOT automatically re-execute your custom navigate() function. You MUST attach a window.addEventListener("popstate", ...) listener to re-render the view when the user traverses their session history.
📝Independent Practice: Assignment
📄 index.htmlUniversity Student Portal 4-View SPA Router
- •Build a multi-view Single Page Application with 4 views: Overview, Lab Practicals, Grades, and Profile.
- •Use hash-based routing (#/overview, #/practicals, #/grades, #/profile).
- •Ensure the active navigation tab receives an emerald green highlight (#04AA6D).
- •Support direct deep-linking: opening the URL directly with #/grades must immediately display the Grades view.
Open Visual Studio Code on your PC (code .), create index.html, paste this complete step-by-step verified code, test in your browser, and push to your GitHub repo:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BCA Day 47 - Student Portal SPA</title>
<style>
body { font-family: "Segoe UI", sans-serif; background: #060c18; color: #fff; padding: 24px; }
nav a { padding: 8px 16px; border-radius: 8px; color: #94a3b8; text-decoration: none; font-weight: bold; margin-right: 8px; }
nav a.active { background: #04AA6D; color: #fff; }
.card { background: #0c1328; border: 1px solid #1e293b; padding: 24px; border-radius: 12px; margin-top: 20px; }
</style>
</head>
<body>
<nav id="menu">
<a href="#/overview" class="active">📊 Overview</a>
<a href="#/practicals">💻 Practicals</a>
<a href="#/profile">👤 Profile</a>
</nav>
<div class="card" id="appContent"></div>
<script>
const views = {
"#/overview": "<h2>Overview</h2><p>Semester 1 progress on track.</p>",
"#/practicals": "<h2>Practicals</h2><p>BCSL-013 Lab Units.</p>",
"#/profile": "<h2>Profile</h2><p>Student: Er. Manoj Kumar BCA Cohort.</p>"
};
function update() {
const hash = location.hash || "#/overview";
document.getElementById("appContent").innerHTML = views[hash] || "<h2>404</h2>";
document.querySelectorAll("#menu a").forEach(a => a.classList.toggle("active", a.getAttribute("href") === hash));
}
window.addEventListener("hashchange", update);
update();
</script>
</body>
</html>🎓4. Viva Check: Mandatory Conceptual Test
Why is the Intersection Observer API drastically more performant than using window.addEventListener("scroll") for lazy-loading images?
5. Mandatory Step: Push to GitHub to Unlock Next Lesson
Target Repo: html-css-projects
Exact GitHub Setup Guide & Expected Structure
Day 47 RequirementsFollow these exact folder and file names to prevent verification errors.
index.html: Avoid spaces or custom names like Alan Turing..html or Index.HTML. Linux/GitHub servers are strictly case-sensitive.day-47/ for today so you don't overwrite previous days.main.You are welcome to read all notes, inspect code snippets, and practice all exercises of Day 47 freely! To officially submit your GitHub repository and unlock subsequent days in your portfolio, please complete Day 46: Browser Geolocation & Device APIs (Permission Models & Coordinate Mapping) first.
Complete Day 46 First