This page is In Progress

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

replaceState

Summary

Overwrites the current history record with a new record.

Method of dom/Historydom/History

Syntax

 history.replaceState(statedata, title, url);

Parameters

statedata

Data-type
any

The data to update.

title

Data-type
String

The data title to update.

url

Data-type
String

(Optional)

The data URL to update.

Return Value

No return value

Examples

In the following example, a user stored a preference where they want to go straight to the User Account page when they go to the homepage of the website. The code checks for such preference and replaces the URL shown by the browser accordingly and shows a User Account page (albeit minimal). This way, there will be no history record for the original homepage of the website, only for the User Account page.

if (localStorage["first-page"] === "account") {
 window.history.replaceState({"page": "account", "User Account", "/account"});
 document.body.innerHTML = "<h1>User Account</h1>";
}

Attributions