admin管理员组

文章数量:1133936

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.

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.

Share Improve this question edited Jan 21, 2020 at 8:50 Pikamander2 8,2994 gold badges54 silver badges73 bronze badges asked Oct 2, 2010 at 18:08 leoraleora 196k367 gold badges906 silver badges1.4k bronze badges
Add a comment  | 

7 Answers 7

Reset to default 220
document.location.href = newUrl;

https://developer.mozilla.org/en-US/docs/Web/API/document.location

Simple assigning to window.location or window.location.href should be fine:

window.location = newUrl;

However, your new URL will cause the browser to load the new page, but it sounds like you'd like to modify the URL without leaving the current page. You have two options for this:

  1. Use the URL hash. For example, you can go from example.com to example.com#foo without loading a new page. You can simply set window.location.hash to make this easy. Then, you should listen to the HTML5 hashchange event, which will be fired when the user presses the back button. This is not supported in older versions of IE, but check out jQuery BBQ, which makes this work in all browsers.

  2. You could use HTML5 History to modify the path without reloading the page. This will allow you to change from example.com/foo to example.com/bar. Using this is easy:

    window.history.pushState("example.com/foo");
    

    When the user presses "back", you'll receive the window's popstate event, which you can easily listen to (jQuery):

    $(window).bind("popstate", function(e) { alert("location changed"); });
    

    Unfortunately, this is only supported in very modern browsers, like Chrome, Safari, and the Firefox 4 beta.

If you just want to update the relative path you can also do

window.location.pathname = '/relative-link'

"http://domain.com" -> "http://domain.com/relative-link"

Hmm, I would use

window.location = 'http://localhost/index.html#?options=go_here';

I'm not exactly sure if that is what you mean.

This will do it:

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

The best way to redirect the user to another URL is by using window.location.assign (See https://developer.mozilla.org/en-US/docs/Web/API/Location/assign).

Nevertheless, if what you want is to change the URL of the page without redirecting the user, then you may use the window.history.replaceState function (See https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState). A combination of this and window.history.pushState is what Single Page Applications (SPAs) use nowadays to keep track of the user navigating throughout the application so that the back button works as expected

You can take a look at the examples in the documentation links I provided to give you an idea on the usage of these functions

本文标签: javascriptHow can I change the current URLStack Overflow