Javascript 如何更改当前 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3846935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:22:02  来源:igfitidea点击:

How can I change the current URL?

javascript

提问by leora

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

我有以下代码可以从 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.

但它不会更改顶部 URL,因此当有人单击后退按钮时,它不会返回上一页。

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

我怎样才能让 JavaScript 更改顶部 URL,以便浏览器后退按钮起作用。

回答by bcherry

Simple assigning to window.locationor window.location.hrefshould be fine:

简单地分配给window.locationwindow.location.href应该没问题:

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:

但是,您的新 URL 将导致浏览器加载新页面,但听起来您希望在不离开当前页面的情况下修改 URL。为此,您有两种选择:

  1. Use the URL hash. For example, you can go from example.comto example.com#foowithout loading a new page. You can simply set window.location.hashto make this easy. Then, you should listen to the HTML5 hashchangeevent, 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/footo example.com/bar. Using this is easy:

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

    When the user presses "back", you'll receive the window's popstateevent, 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.

  1. 使用 URL 哈希。例如,您可以example.comexample.com#foo不加载新页面的情况下从到。您可以简单地设置window.location.hash以简化此操作。然后,您应该收听 HTML5hashchange事件,该事件将在用户按下后退按钮时触发。这在旧版本的 IE 中不受支持,但请查看jQuery BBQ,它可以在所有浏览器中使用。

  2. 您可以使用 HTML5 History 修改路径而无需重新加载页面。这将允许您从 更改example.com/fooexample.com/bar。使用它很容易:

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

    当用户按下“返回”时,您将收到窗口的popstate事件,您可以轻松地收听(jQuery):

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

    不幸的是,这仅在非常现代的浏览器中受支持,例如 Chrome、Safari 和 Firefox 4 beta。

回答by Artokun

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

如果您只想更新相对路径,您也可以这样做

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

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

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

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

回答by Blender

Hmm, I would use

嗯,我会用

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

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

我不确定这是否是你的意思。

回答by user3230794

<script> 
    var url= "http://www.google.com"; 
    window.location = url; 
</script>