JavaScript | Replace() Method

The replace() method in JavaScript is used to replace the current page with another page. The Replace Method replaces the URL of the current window by the URL mention in the Replace method.

Syntax:

location.replace(URL)

Parameter: This method accepts single parameter URL. The URL is the reference of other page which need to replace with older page.

Return Value: This method returns or open the new page in window.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
     <head>
          <title>Redirect to Webpage</title>
          <style>
            body{
                text-align:center;
            }
            .gfg {
                font-size:40px;
                font-weight:bold;
                color:green;
            }
          </style>
     </head>
     <body>
     <div class = "gfg">GeeksforGeeks</div>
     <h2>Replace Method</h2>
     <button onclick = "Replace()" >Replace with Gfg</button>
          <script>
  
               //Replace function that replace the current page.
               function Replace() {
                    location.replace("https://www.geeksforgeeks.org/")
               }
  
          </script>
     </body>
</html>

chevron_right


Output:
Output:-
Before clicking on the button:

After clicking on the button:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.