JS Coding Questions Logo
JS Coding Questions
#285💼 Interview💻 Code

How do I modify the url without reloading the page

Advertisement

728x90

The window.location.href property will be helpful to modify the url but it reloads the page. HTML5 introduced the history.pushState() and history.replaceState() methods, which allow you to add and modify history entries, respectively. For example, you can use pushState as below,

javascript
1window.history.pushState("page2", "Title", "/page2.html");

This mechanism is used by routing libraries of frameworks like React and Angular in order to simulate the behaviour of a multi-page-website, even though they are only SPA (Single Page Applications).

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 29

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
285of476