HTML | DOM Location replace() Method

The DOM Location replace() Method in HTML is used to replace the current document with the specified one. This method is different from the assign() method as this removes the current document from the document history, therefore it is not possible to go back to the previous document using the ‘back’ button.

Syntax:

location.replace( newUrl );

Parameters:



  • newUrl: This is the URL to replace the current page with. This is a required parameter.

Note: The URL to be replaced must allow framing.

Return Value: No return value.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<head>
    <title>DOM Location replace() Method</title>
</head>
  
<body>
    <h1 style="color: green">GeeksforGeeks</h1>
    <b>DOM Location replace() Method</b>
  
    <p>Click the button to replace 
      the current document with a new one.</p>
  
    <button onclick="changePage()">
      Change Page
    </button>
  
    <script>
        function changePage() {
  
            // change the document to
            // the GeeksforGeeks homepage
            location.replace(
              "https://ide.geeksforgeeks.org");
        }
    </script>
</body>
  
</html>

chevron_right


Note: Run the above code on ide.geeksforgeeks.org as the link used in the code is ide.geeksforgeeks.org to allow framing.

Output:

Before pressing the button:
before-click

After pressing the button:
after-click

Supported Browsers: The browser supported by DOM Location replace() Method are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Technical Writer Lets have an update

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.