docs.rodeo

MDN Web Docs mirror

History: pushState() method

{{APIRef("History API")}} 

The pushState() method of the {{domxref("History")}}  interface adds an entry to the browser’s session history stack.

Syntax

pushState(state, unused)
pushState(state, unused, url)

Parameters

Return value

None ({{jsxref("undefined")}} ).

Exceptions

Description

In a sense, calling pushState() is similar to setting window.location = "#foo", in that both will also create and activate another history entry associated with the current document. But pushState() has a few advantages:

Note that pushState() never causes a {{domxref("Window/hashchange_event", "hashchange")}}  event to be fired, even if the new URL differs from the old URL only in its hash.

Examples

This creates a new browser history entry setting the state and url.

JavaScript

const state = { page_id: 1, user_id: 5 };
const url = "hello-world.html";

history.pushState(state, "", url);

Change a query parameter

const url = new URL(location);
url.searchParams.set("foo", "bar");
history.pushState({}, "", url);

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN