The onpopstate attribute handles the window popstate event. It runs when the active session history entry changes — usually Back/Forward, or history.back() / history.forward() / history.go().
Put it on <body>, or use window.addEventListener("popstate", …). Pair with history.pushState() / replaceState() for same-document navigation. Related: onhashchange for # fragment changes.
Baseline · Widely available.
Stable across modern browsers since about July 2015. Not deprecated.
Inline handlers discouraged.
MDN: prefer addEventListener. Note: pushState / replaceState do not fire popstate — navigating to those entries later does.
Syntax
HTML Syntax
syntax.html
<body onpopstate="script">
script runs when popstate fires. Preferred: window.addEventListener("popstate", handler). Use event.state for the History API state object.
Use Try It Yourself for a clean history stack, or the live preview above.
How It Works
onpopstate on body registers a window-level handler.
pushState adds a history entry (no popstate yet).
history.back() activates the previous entry.
popstate fires; event.state reflects that entry.
Example 2 — Preferred addEventListener
Same event with a clean script listener.
HTML
<p id="status">Push, then Back or Forward.</p>
<button type="button" id="pushBtn">Push</button>
<button type="button" id="backBtn">Back</button>
<script>
var n = 0;
var status = document.getElementById("status");
window.addEventListener("popstate", function (event) {
status.textContent = "popstate — state: " + JSON.stringify(event.state);
});
document.getElementById("pushBtn").addEventListener("click", function () {
n += 1;
history.pushState({ step: n }, "", "?step=" + n);
status.textContent = "Pushed step " + n + " (no popstate yet).";
});
document.getElementById("backBtn").addEventListener("click", function () {
history.back();
});
</script>
popstate reads event.state.step and restores the label.
Pattern: store UI data in state; re-render on traversal.
Edge Cases & Pitfalls
Most confusion is expecting pushState to fire popstate.
push
pushState ≠ popstate
Calling pushState / replaceState does not fire the event. Traversing to that entry later does.
window
Window-level event
Listen on window (or body for the HTML attribute). A random div will not help.
interact
May need user interaction
Some browsers delay or suppress history-related events until the page has been interacted with.
hash
vs hashchange
Fragment-only changes are hashchange territory. Same-document History API work usually uses popstate + event.state.
style
Prefer listeners
Inline on* attributes mix concerns. Use addEventListener in production code.
Compatibility
Browser Support
onpopstate is a window event handler for the popstate / History API. Prefer addEventListener in apps.
✓ Baseline · Widely available
onpopstate
Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support popstate on window.
97%Global support (approx.)
Google Chrome5+
Full support
Mozilla Firefox4+
Full support
Apple Safari5+
Full support
Microsoft Edge12+
Full support
Opera11.5+
Full support
Samsung InternetModern
Full support
Internet Explorer10+
Supported*
popstateWidely supported
Bottom line: *Safe on modern browsers. Remember: pushState does not fire popstate — Back/Forward (or history.back) does.
Remember
Key Takeaways
Role: react when session history navigation changes the active entry.
Trigger: Back/Forward / history.back — not pushState alone.
Data: read event.state from History API entries.
Target:window / body.
One line:onpopstate runs on history traversal — store UI in pushState state, restore it when popstate fires.
Frequently Asked Questions
It is an event handler for the window popstate event. That event fires when the active session history entry changes — typically when the user presses Back or Forward, or when script calls history.back(), history.forward(), or history.go().
No. pushState and replaceState change the history stack without firing popstate. popstate runs when you navigate to an existing entry (Back/Forward or history.back/forward/go).
It is a window-level handler. In HTML, put it on body (window handlers on body listen on window), or use window.addEventListener("popstate", …) / window.onpopstate.
If the activated history entry was created or updated with pushState/replaceState, event.state is a copy of that entry’s state object. Otherwise it may be null.
Prefer window.addEventListener("popstate", handler). MDN discourages inline HTML event handler attributes.
Both can fire around history changes. hashchange is specifically about URL fragment (#) changes. In the navigation sequence, popstate is sent before hashchange when both apply.