What is the History API?
The History API (window.history) lets JavaScript move through the browser's session history and change the URL without reloading the page, using pushState, replaceState and the popstate event; it is the foundation of client-side routing.
Methods
history.pushState(state, "", url): adds a new entry and changes the address bar. No request is made and nopopstatefires.history.replaceState(state, "", url): changes the current entry instead of adding one, useful for updating query params (filters, search) without growing the back stack.history.back(),forward(),go(n): navigate like the browser buttons.history.state: the state object of the current entry.history.length: number of entries in this tab's session.
The popstate event
Fires when the user navigates with back/forward (or you call back/go). Your router reads location and event.state and renders the matching view.
Rules
- URLs must be same-origin.
- The
stateobject must be structured-cloneable and is limited in size. - After
pushState, the server must be able to handle that URL on a refresh (serve the app shell for all routes).
Navigation API
A newer API, window.navigation, gives a single navigate event for all navigations (links, forms, back/forward) and is supported in Chromium browsers and recent Safari and Firefox releases.
In frameworks
React Router, Next.js and others wrap this. In Next.js, window.history.pushState/replaceState also integrate with the router.
Code example
1document.body.innerHTML = `<nav><a href="/home" data-link>Home</a> <a href="/about" data-link>About</a></nav><main id="view"></main>`;2const view = document.getElementById("view");3const routes = { "/home": "Welcome home", "/about": "About us" };45function render(path) {6 view.textContent = routes[path] ?? "Not found";7}89// Change the URL without reloading. Sandboxed iframes (like the playground preview) forbid10// URL changes entirely, so routing keeps working there, just without updating the address.11function navigate(path, replace = false) {12 try {13 history[replace ? "replaceState" : "pushState"]({ path }, "", path);14 } catch {15 console.warn("history API blocked in this sandboxed preview - open on a normal page to see URL changes");16 }17 render(path);18}1920// Intercept link clicks: change the URL without reloading21document.addEventListener("click", (e) => {22 const link = e.target.closest("a[data-link]");23 if (!link) return;24 e.preventDefault();25 navigate(link.getAttribute("href"));26});2728// Back/forward buttons29window.addEventListener("popstate", (e) => render(e.state?.path ?? location.pathname));3031document.querySelector('a[href="/about"]').click();32console.log(view.textContent); // About us (and on a normal page: location.pathname === "/about")3334// replaceState: update the query string without adding a history entry35const params = new URLSearchParams(location.search);36params.set("sort", "newest");37try {38 history.replaceState(history.state, "", `?${params}`);39 console.log(location.search); // ?sort=newest40} catch {41 console.log("would update the query to", `?${params}`);42}
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.