History API
In one line
The History API lets you read and modify the browser's session history stack, enabling SPA navigation without full reloads.
In simple words
The History API gives JavaScript access to the browser's session history — the stack of pages the user has visited. You can move back and forward programmatically, and crucially, push new entries onto the stack without triggering a full page navigation. This is the foundation of single-page application (SPA) routing.
The key methods are pushState (add a new history entry with a state object and URL) and replaceState (replace the current entry). Both change the URL in the address bar without reloading the page. When the user clicks back/forward, the popstate event fires, and your code can react by rendering the appropriate view.
Combined with the fetch API, this lets you build apps that feel like multi-page sites but never do a full document reload — the URL changes, the view updates, and the back button works. Frameworks like React Router and Vue Router wrap this API.
Example
// Push a new "page" without reloading
function navigateTo(path) {
history.pushState({ path }, "", path);
renderView(path); // your function to update the DOM
}
// Handle back/forward button
window.addEventListener("popstate", (e) => {
renderView(e.state?.path || "/");
});
navigateTo("/users"); // URL becomes /users, no reloadpushState adds a history entry and changes the URL. popstate fires on back/forward, letting you re-render the matching view.
Related terms
Related Resources
Related Lessons
- SPA Routing
Learn SPA navigation.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.