W3cubDocs

/Web APIs

Location: replace() 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 replace() method of the Location interface replaces the current resource with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session History, meaning the user won't be able to use the back button to navigate to it. Not to be confused with the String method String.prototype.replace().

Syntax

replace(url)

Parameters

url

A string or any other object with a stringifier, such as a URL object, containing the URL of the page to navigate to.

Exceptions

SecurityError DOMException

Browsers throttle navigations and may throw this error, generate a warning, or ignore the call if it's called too frequently.

SyntaxError DOMException

Thrown if the provided url parameter is not a valid URL.

Return value

None (undefined).

Examples

// Navigate to the Location.reload article by replacing this page
window.location.replace(
  "https://developer.mozilla.org/en-US/docs/Web/API/Location.reload",
);

Specifications

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
replace 1 12 1 3 1 18 4 10.1 1 1.0 4.4 1

See also

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Location/replace