HTML onpopstate Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

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.

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.

Attribute Values

How you listen for popstate:

Value / formDescriptionTry it
JavaScript on bodyInline handler (study form)Try it
addEventListener("popstate")Preferred modern listenerTry it
Read event.stateRestore UI from pushState dataTry it

Live Preview

Push a demo history entry, then go Back. popstate fires on Back/Forward — not on Push alone.

history

Demo entries use a ?ops= query so you can see the URL change. Click the page first if your browser delays history events.

Demo

Ready. Click Push, then Back.

HTML Examples

Short study snippets. Use Try It Yourself for a live editor. Prefer listeners in real apps.

Example 1 — Inline onpopstate on body

Recognize the attribute form. Push, then Back to fire the handler.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>onpopstate demo</title>
</head>
<body onpopstate="document.getElementById('status').textContent = 'popstate fired. state=' + JSON.stringify(event.state)">
  <p id="status">Click Push, then Back.</p>
  <button type="button" onclick="history.pushState({step:1}, '', '?step=1')">Push</button>
  <button type="button" onclick="history.back()">Back</button>
</body>
</html>
Try It Yourself

How It Works

  1. onpopstate on body registers a window-level handler.
  2. pushState adds a history entry (no popstate yet).
  3. history.back() activates the previous entry.
  4. 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>
Try It Yourself

How It Works

  1. Listener on window — no inline attribute.
  2. Push updates the URL/query and stores { step: n }.
  3. Status notes that push alone skips popstate.
  4. Back traverses history and fires the listener.

Example 3 — Update UI from event.state

Simple same-document “steps” restored when the user presses Back/Forward.

HTML
<p id="view">Step 0</p>
<button type="button" id="nextBtn">Next step</button>
<button type="button" id="backBtn">Back</button>

<script>
  var view = document.getElementById("view");
  var step = 0;

  function show(s) {
    step = s;
    view.textContent = "Step " + step;
  }

  window.addEventListener("popstate", function (event) {
    show(event.state && event.state.step != null ? event.state.step : 0);
  });

  document.getElementById("nextBtn").addEventListener("click", function () {
    show(step + 1);
    history.pushState({ step: step }, "", "?step=" + step);
  });

  document.getElementById("backBtn").addEventListener("click", function () {
    history.back();
  });
</script>
Try It Yourself

How It Works

  1. Next updates the view and pushStates the step.
  2. Back moves to the previous history entry.
  3. popstate reads event.state.step and restores the label.
  4. 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.

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 Chrome 5+
Full support
Mozilla Firefox 4+
Full support
Apple Safari 5+
Full support
Microsoft Edge 12+
Full support
Opera 11.5+
Full support
Samsung Internet Modern
Full support
Internet Explorer 10+
Supported*
popstate Widely supported

Bottom line: *Safe on modern browsers. Remember: pushState does not fire popstate — Back/Forward (or history.back) does.

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.

Next: onprogress

Track media download progress as data arrives.

onprogress tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful