How to modify URL without reloading the page using JavaScript ? - GeeksforGeeks

31 Oct, 2019
Method 1: Replacing the current state with replaceState() Method: The 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:
<!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:
Before clicking the button:
After clicking the button:
Method 2: Adding a new state with pushState() Method: The 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:
<!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:
Before clicking the button:
After clicking the button:
JavaScript-Misc
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?