Javascript Reference - Location replace() Method








The replace() method replaces the current document with a new one from the specified URL.

replace() removes the URL of the current document from the document history and we cannot use the "back" button to navigate back to the original document.

assign() method loads a new document to replace the current one and we have the option to navigate back to the original document.

Browser Support

replace() Yes Yes Yes Yes Yes




Syntax

location.replace(newURL)

Parameter Values

Parameter Type Description
newURL String Required. the URL of the page to go to

Return Value

No return value.

Example

The following code shows how to replace the current document.


<!DOCTYPE html>
<html>
<body>
<!--  w w  w .  ja  v a2 s. c om-->
<button onclick="myFunction()">Replace document</button>

<script>
function myFunction() {
    location.replace("http://www.example.com")
}
</script>

</body>
</html> 

The code above is rendered as follows: