Javascript Browser Location replace() Method

Introduction

Replace the current document:

location.replace("https://www.java2s.com");

View in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Replace document</button>

<script>
function myFunction() {//from ww  w  . j a  v a  2s  .co  m
  location.replace("https://www.java2s.com")
}
</script>

</body>
</html>

The replace() method replaces the current document with a new document.

The replace() method removes the URL of the current document from the document history.

It is not possible to use the "back" button to navigate back to the original document.

Use the assign() method to load a new document, and have the option to navigate back to the original document.

location.replace(newURL);

Parameter Values

Parameter Type Description
newURLString Required. Specifies the URL of the page to navigate to

The replace() method has No return value.




PreviousNext

Related