How can I change the current URL?

Viewed 284754

I have the following code that changes the pages from within JavaScript:

var newUrl = [some code to build up URL string];
window.location.replace(newUrl);

But it doesn't change the top URL, so when someone clicks the back button it doesn't go back to the previous page.

How can I have JavaScript change the top URL as well so the browser back button works.

6 Answers

This will do it:

window.history.pushState(null,null,'https://www.google.com');
<script> 
    var url= "http://www.google.com"; 
    window.location = url; 
</script> 
Related