History: replaceState() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

The replaceState() method of the History interface modifies the current history entry, replacing it with the state object and URL passed in the method parameters. This method is particularly useful when you want to update the state object or URL of the current history entry in response to some user action.

Syntax

js
replaceState(state, unused)
replaceState(state, unused, url)

Parameters

state

An object which is associated with the history entry passed to the replaceState() method. The state object can be null.

unused

This parameter exists for historical reasons, and cannot be omitted; passing the empty string is traditional, and safe against future changes to the method.

url Optional

The URL of the history entry. The new URL must be of the same origin as the current URL; otherwise the replaceState() method throws an exception.

Return value

None (undefined).

Exceptions

SecurityError DOMException

Thrown if the associated document is not fully active, or if the provided url parameter is not a valid URL, or if the method is called too frequently.

DataCloneError DOMException

Thrown if the provided state parameter is not serializable.

Examples

Suppose https://www.mozilla.org/foo.html executes the following JavaScript:

js
const stateObj = { foo: "bar" };
history.pushState(stateObj, "", "bar.html");

On the next page you could then use history.state to access the stateObj that was just added.

The explanation of these two lines above can be found in the Working with the History API article. Then suppose https://www.mozilla.org/bar.html executes the following JavaScript:

js
history.replaceState(stateObj, "", "bar2.html");

This will cause the URL bar to display https://www.mozilla.org/bar2.html, but won't cause the browser to load bar2.html or even check that bar2.html exists.

Suppose now that the user navigates to https://www.microsoft.com, then clicks the Back button. At this point, the URL bar will display https://www.mozilla.org/bar2.html. If the user now clicks Back again, the URL bar will display https://www.mozilla.org/foo.html, and totally bypass bar.html.

Specifications

Specification
HTML
# dom-history-replacestate-dev

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
replaceState
Chrome – Full support
Chrome 5 (Release date: 2010-05-25)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 4 (Release date: 2011-03-22)
footnote
footnote Until Firefox 5, the passed object is serialized using JSON. Starting in Firefox 6, the object is serialized using the structured clone algorithm. This allows a wider variety of objects to be safely passed.
Opera – Full support
Opera 11.5 (Release date: 2011-06-28)
footnote Full support
Safari – Full support
Safari 5 (Release date: 2010-06-07)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote
footnote Until Firefox for Android 5, the passed object is serialized using JSON. Starting in Firefox for Android 6, the object is serialized using the structured clone algorithm. This allows a wider variety of objects to be safely passed.
Opera Android – Full support
Opera Android 11.5 (Release date: 2011-10-12)
footnote Full support
Safari on iOS – Full support
Safari on iOS 4 (Release date: 2010-06-21)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 4 (Release date: 2010-06-21)
footnote Full support
Whether the unused parameter is used
Deprecated Non-standard
Chrome – No support
Chrome
footnote No support
Edge – No support
Edge
footnote No support
Firefox – No support
Firefox
footnote No support
Opera – No support
Opera
footnote No support
Safari – No support
Safari 5 – 17.6 (Release date: 2010-06-07)
footnote Removed in 18 and later
footnote See bug 223190.
Chrome Android – No support
Chrome Android
footnote No support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – No support
Opera Android
footnote No support
Safari on iOS – No support
Safari on iOS 4 – 17.6 (Release date: 2010-06-21)
footnote Removed in 18 and later
footnote See bug 223190.
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – No support
WebView on iOS 4 – 17.6 (Release date: 2010-06-21)
footnote Removed in 18 and later
footnote See bug 223190.

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Non-standard. Check cross-browser support before using.
Deprecated. Not for use in new websites.
See implementation notes.