A Quick guide about popstate event in JavaScript, If you’ve ever hit the back button in your browser and wondered how your Single-Page Application knows which view to render, this guide is for you.
Vijay Sai Krishna vsuri
Last Updated Feb 21, 2026

popstate in Single Page Applications (SPAs)We'll demystify the popstate event, explore examples in vanilla JavaScript, and then compare how libraries like React Router handle it under the hood.
popstate in JavaScript?The popstate event is fired on the window whenever the active history entry changes. This usually happens when the user clicks Back or Forward in the browser
window.addEventListener("popstate", (event) => {
console.log("Location changed to:", window.location.pathname);
console.log("State object:", event.state);
});
hashchange, popstate deals with the History API (pushState, replaceState, back, forward, go).pushState or replaceState directly.history.back(), history.forward(), etc.Imagine a single-page app with two pages:
<button id="page1">Go to Page 1</button>
<button id="page2">Go to Page 2</button>
<div id="content"></div>
const content = document.getElementById("content");
function renderPage(page) {
content.textContent = `You are on ${page}`;
}
document.getElementById("page1").onclick = () => {
history.pushState({ page: "Page 1" }, "", "/page1");
renderPage("Page 1");
};
document.getElementById("page2").onclick = () => {
history.pushState({ page: "Page 2" }, "", "/page2");
renderPage("Page 2");
};
window.addEventListener("popstate", (event) => {
if (event.state) {
renderPage(event.state.page);
}
});
postMessage for syncing history between origins.pushState/replaceState don’t trigger popstate.To synchronize navigation across iframes, use a communication layer with postMessage.
Question:
👉 "If I open /page2 directly in the browser, how does my SPA know what to render?"
/page2.htmlindex.html (History API Fallback)index.html loads:window.location.pathname<BrowserRouter>
<Routes>
<Route path="/page1" element={<Page1 />} />
<Route path="/page2" element={<Page2 />} />
</Routes>
</BrowserRouter>
Opening/page2loadsindex.html, then React Router handles the routing.
❌ If the server isn’t configured: You’ll get a 404 error, since the server looks for /page2/index.html and doesn’t find it.
✅ Solution: Configure the server to always return index.html.
React Router (v6 and above) leverages the History API just like your vanilla JS examples, but with abstraction:
<Link> and hooks like useNavigate()popstate and re-renders the correct component treeimport { BrowserRouter, Routes, Route, Link } from "react-router-dom";
export default function App() {
return (
<BrowserRouter>
<nav>
<Link to="/page1">Page 1</Link>
<Link to="/page2">Page 2</Link>
</nav>
<Routes>
<Route path="/page1" element={<div>You are on Page 1</div>} />
<Route path="/page2" element={<div>You are on Page 2</div>} />
</Routes>
</BrowserRouter>
);
}
React Router saves you from writing boilerplate like:
popstatelocation.pathnamepopstate is triggered on browser history navigation, not on pushState/replaceStatepopstate is scoped to the iframe/windowindex.html (History API fallback)window.location.pathnameBe the first to share your thoughts!
No comments yet.
Start the conversation!
Build Your Portfolio
Help the Community
Strengthen Your Skills
Share your knowledge by writing a blog or quick notes. Your contribution can help thousands of frontend developers ace their interviews and grow their careers! 🚀
Kirtesh Bansal
Last Updated Feb 21, 2026
A beginner-friendly guide to understanding call, apply, and bind methods in JavaScript, along with step-by-step call, apply and bind polyfill implementations that are often asked in interviews.
Anuj Sharma
Last Updated Aug 3, 2025
Explore the implementation of setTimeout in JavaScript with a detailed explanation for every step. Understand all scenarios expected to implement the setTimeout polyfill.
Anuj Sharma
Last Updated Nov 15, 2025
Understand the code implementation of useSessionStorage custom hook in react that will help to efficiently manager session storage in application.
Anuj Sharma
Last Updated Feb 21, 2026
Find the top React Performance Optimization Techniques specific to React applications that help to make your react app faster and more responsive for the users along with some bonus techniques.
Anuj Sharma
Last Updated Feb 21, 2026
Explore code explanation of useToggle() custom hook in react to handle the toggle event efficiently.
Anuj Sharma
Last Updated Feb 21, 2026
Easy to understand 5 rules, that cover the behaviour of the "this" keyword in different contexts and helps you to master this keyword for any javascript interview.
Subscribe to FrontendGeek Hub for frontend interview preparation, interview experiences, curated resources and roadmaps.
All in One Preparation Hub to Ace Frontend Interviews. Master JavaScript, React, System Design, and more with curated resources.
© 2026 FrontendGeek. All rights reserved