Javascript 如何将 window.location 设置为特定路径(没有主机)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10109360/
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
How do I set window.location to a specific path (without a host)?
提问by jimbouton
I am using the window location method to redirect a webpage to another after a set amount of time.
我正在使用窗口定位方法在一段时间后将网页重定向到另一个网页。
The url needs to change from www.myurl.com/home to www.myurl.com/other. The problem is that I do not know what the final URLs will be so I cannot use absolute links, they have to be a path only. This is what I have so far:
url 需要从 www.myurl.com/home 更改为 www.myurl.com/other。问题是我不知道最终的 URL 是什么,所以我不能使用绝对链接,它们只能是路径。这是我到目前为止:
window.location.pathname = "mobility.html"
回答by bfavaretto
You can just prepend a /
to your URL to make them relative to the domain root (without having to hardcode the domain name). Like this:
您可以/
在您的 URL前面加上 a以使它们相对于域根(无需对域名进行硬编码)。像这样:
window.location = "/mobility.html"
回答by Shaun Luttin
window.location.assign("/path")
also works.
window.location.assign("/path")
也有效。