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:
<!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() { } </script> </body> </html> |
Output:
Output:-
Before clicking on the button:

After clicking on the button:

Recommended Posts:
- JavaScript | string.replace()
- How to replace all dots in a string using JavaScript?
- How to replace plain URL with link using JavaScript ?
- How to replace a character at a particular index in JavaScript ?
- How to replace an item from an array in JavaScript?
- How to replace lowercase letters with an other character in JavaScript ?
- Replace special characters in a string with underscore (_) in JavaScript
- How to globally replace a forward slash in a JavaScript string ?
- HTML | DOM Location replace() Method
- JavaScript | Replace multiple strings with multiple other strings
- JavaScript | Array.from() method
- JavaScript | Promise.all() Method
- JavaScript | padEnd() Method
- JavaScript | Array map() Method
- JavaScript | Sort() method
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.


