The History API allows JavaScript to manipulate the browser's session history without reloading the page. One of its most important methods is history.pushState(), which adds a new history entry and updates the URL.
π What is pushState()?
history.pushState() adds a new entry to the browser's history stack. It changes the URL displayed in the address bar without refreshing the page, making it a key feature for building Single Page Applications (SPAs).
π‘ Why Use pushState()?
- β‘ Build Single Page Applications (SPAs)
- π Navigate between pages without reloading
- π Create shareable URLs
- π Improve browser Back and Forward navigation
- π― Maintain application state in the URL
π Syntax
pushState() Syntax
history.pushState(state, unused, url);π Parameters
| Parameter | Description |
|---|---|
| state | A JavaScript object associated with the history entry. |
| unused | Historically used for the page title. Pass an empty string (""). |
| url | The new URL (must be from the same origin). |
π Basic Example
Change URL
history.pushState(
{},
"",
"/about"
);The browser URL becomes /about, but the page does not reload.
π¦ Storing State
You can associate data with the history entry using thestate object.
Store State
history.pushState(
{
page: "about",
id: 101
},
"",
"/about"
);π Reading State
Access the current history entry's state usinghistory.state.
Read State
console.log(history.state);β¬ οΈ Handling Back & Forward Buttons
When the user navigates using the browser's Back or Forward buttons, thepopstate event is triggered.
popstate Event
window.addEventListener(
"popstate",
(event) => {
console.log(event.state);
}
);π Complete Navigation Example
HTML
<button id="homeBtn">Home</button>
<button id="aboutBtn">About</button>
<h2 id="content">
Home Page
</h2>JavaScript
const content =
document.getElementById("content");
document
.getElementById("homeBtn")
.onclick = () => {
history.pushState(
{ page: "home" },
"",
"/home"
);
content.textContent =
"Home Page";
};
document
.getElementById("aboutBtn")
.onclick = () => {
history.pushState(
{ page: "about" },
"",
"/about"
);
content.textContent =
"About Page";
};
window.addEventListener(
"popstate",
(event) => {
if (event.state?.page === "home") {
content.textContent =
"Home Page";
}
if (event.state?.page === "about") {
content.textContent =
"About Page";
}
}
);π pushState() vs replaceState()
Both methods update the browser's history, but they behave differently.
| Feature | pushState() | replaceState() |
|---|---|---|
| Creates New History Entry | β Yes | β No |
| Changes URL | β Yes | β Yes |
| Back Button Returns to Previous URL | β Yes | β No (the current entry is replaced) |
π History Object Methods
| Method | Description |
|---|---|
| pushState() | Adds a new history entry. |
| replaceState() | Replaces the current history entry. |
| back() | Moves back one page. |
| forward() | Moves forward one page. |
| go() | Moves through history by a specified number of entries. |
π§ͺ More Examples
Navigate Back
history.back()
history.back();Navigate Forward
history.forward()
history.forward();Go Two Pages Back
history.go()
history.go(-2);Reload Current Entry
Reload Current Entry
history.go(0);β οΈ Limitations
- π The new URL must belong to the same origin (same protocol, domain, and port).
- π pushState() changes only the URLβit does not load new HTML automatically.
- π§ You are responsible for updating the page content after changing the URL.
- π Refreshing a pushed URL requires appropriate server-side routing if the URL doesn't map to a physical file.
Note
β Best Practices
- πΊοΈ Keep the URL synchronized with the application's visible state.
- π¦ Store only lightweight, serializable data in the state object.
- β¬ οΈ Handle the popstate event so Back and Forward navigation works correctly.
- π Use meaningful URLs that users can bookmark and share.
- π Configure your server to support client-side routes on page refresh.
π― Summary
history.pushState() is a core feature of the History API that lets you add new history entries, update the browser's URL, and associate state with each entryβall without reloading the page. Combined with thepopstate event, it enables smooth client-side navigation and forms the foundation of modern Single Page Applications.