How to modify URL without reloading the page using JavaScript ? Last Updated : 18 Dec, 2023 Comments Improve Suggest changes Like Article Like Report In this article, we are going to see how to modify URL without reloading the page using JavaScript Below are the methods to modify the URL without reloading the page using JavaScript: Table of Content Replacing the current state with replaceState() MethodAdding a new state with pushState() MethodMethod 1: Replacing the current state with replaceState() MethodThe history interface of the browser manages the browser session history. It includes the page visited in the tab or frame where the current page is located. Manipulating this state can be used to change the URL of the browser without reloading the page.The replaceState() method is used to modify the current history entry and replace the properties of the state with ones in the passed parameters.The parameters of this method have 3 parts, the state object which is a serializable object about the state, the title which is the title of the new state, and the URL which is the new URL of the state.The URL can be changed by passing the required URL as a string to this method. This will change the URL of the page without reloading the page. Example: In this example, we have used the replaceState() method. html <!DOCTYPE html> <html> <head> <title> How to modify URL without reloading the page using JavaScript? </title> </head> <body> <h1 style="color: green"> GeeksForGeeks </h1> <b> How to modify URL without reloading the page using JavaScript? </b> <p> Click on the button below to modify the current history state </p> <button onclick="modifyState()"> Modify history state </button> <script> function modifyState() { let stateObj = { id: "100" }; window.history.replaceState(stateObj, "Page 3", "/page3.html"); } </script> </body> </html> Output: Method 2: Adding a new state with pushState() MethodThe pushState() method is used to add a new history entry with the properties passed as parameters. This will change the current URL to the new state given without reloading the page.The parameters of this method have three parts, the state object which is a serializable object about the state, the title which is the new title of the state and the URL which is the new URL of the state.The URL can be changed by passing the required URL as a string to this method. This will change the URL of the page without reloading the page. Example: In this example, we have used pushState() method. html <!DOCTYPE html> <html> <head> <title> How to modify URL without reloading the page using JavaScript? </title> </head> <body> <h1 style="color: green"> GeeksForGeeks </h1> <b> How to modify URL without reloading the page using JavaScript? </b> <p> Click on the button below to add a new history state </p> <button onclick="addState()"> Add history state </button> <script> function addState() { let stateObj = { id: "100" }; window.history.pushState(stateObj, "Page 2", "/page2.html"); } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to modify URL without reloading the page using JavaScript ? sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to reload CSS without reloading the page using JavaScript ? While working with CSS, you might have come across situations where you made some changes in the stylesheet and had to do a hard reload to see the changes reflected in your browser. Or maybe the style depends on some user interaction and you don't wish to hard reload the page every time. Sometimes y 2 min read How to replace plain URL with link using JavaScript ? Given a plane URL, the task is to replace the plain URLs with the links. This problem can be solved with the help of Regular Expressions. Approach:Â Using RegExp and replace() MethodRegExp - This looks for the URL in the provided text.This RegExp parses the URL and puts the address to the $1 variable 2 min read JavaScript method to get the URL without query string The task is to get the URL name of the page without using a query string with the help of JavaScript. replace() method: This method searches a string for a defined value, or a regular expression, and returns a new string with the replaced defined value. Syntax: string.replace(searchVal, newvalue) Pa 3 min read How to parse URL using JavaScript ? Given an URL and the task is to parse that URL and retrieve all the related data using JavaScript. Example: URL: https://www.geeksforgeeks.org/courses When we parse the above URL then we can find hostname: geeksforgeeks.com path: /courses Method 1: In this method, we will use createElement() method 2 min read How to Display Changed Browser URL Without Reloading Through alert using JavaScript ? To change the URL in the browser without loading the new page, we can use history.pushState() method and replaceState() method from JavaScript. To display the browser-URL before changing the URL we will use window.location.href in the alert() function and will use again after changing the browsers-U 1 min read How to get URL Parameters using JavaScript ? To get URL parameters using JavaScript means extracting the query string values from a URL. URL parameters, found after the ? in a URL, pass data like search terms or user information. JavaScript can parse these parameters, allowing you to programmatically access or manipulate their values.For getti 3 min read How to get the file name from page URL using JavaScript ? JavaScript provides multiple techniques for string manipulation and pattern matching. By demonstrating various methods, the article equips developers with versatile solutions to dynamically retrieve and utilize file names from different URL formats within their applications. There are several approa 3 min read How to open URL in a new window using JavaScript? In HTML, the anchor tag (<a>) is used to open new windows and tabs in a very straightforward manner. However, there are situations where you need to achieve the same functionality using JavaScript. This is where the window.open() method becomes useful.The window.open() method is used to open a 3 min read How to close window using JavaScript which is opened by the user with a URL ? JavaScript does not allow one to close a window opened by the user, using the window.close() method due to security issues. However, we can close a window by using a workaround. The approach to be followed is by opening the current URL using JavaScript so that it could be closed with a script.Syntax 2 min read How to reload page only once in JavaScript ? Reloading a webpage once using JavaScript ensures that users see the most up-to-date content on their initial visit. This technique leverages session storage to track if the page has been reloaded, enhancing user experience by preventing unnecessary reloads and ensuring content freshness. Using Loca 3 min read Like