Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

PopStateEvent.state — history entry state

PopStateEvent.state reads the state associated with the history entry activated during a popstate event. Learn what null means and why saving state is different from navigating through history.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

event.state

PopStateEvent.state is a read-only property accessed as event.state. For a navigation event, it contains a copy of the data saved by history.pushState() or history.replaceState() in the entry being activated, not the entry being left. Missing saved state means null; check for it before reading fields.

Calling history.pushState() or history.replaceState() alone does not fire popstate. The event is associated with history traversal, such as using the Back button. A browser may require prior interaction with the page. In an application, receive it through window.addEventListener("popstate", handler) and restore the view using event.state.

The example deliberately creates two PopStateEvent objects and passes them directly to a function. It neither dispatches events nor changes history. The state option supplies the first object's data; omitting it produces null. The expected lines are {"page":2} and null.

This is a browser Web API defined by the HTML standard, not a feature of a particular ECMAScript edition. MDN reports broad availability for the property since July 2015 and for the constructor used in the example since August 2016. Having Node 24 in a repository does not establish support for these interfaces.

Examples

(() => {
  function describeState(event) {
    console.log(JSON.stringify(event.state));
  }

  describeState(new PopStateEvent("popstate", { state: { page: 2 } }));
  describeState(new PopStateEvent("popstate"));
})();

Result

{"page":2}
null

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources