如何使用 window.location.replace javascript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8293983/
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 to use window.location.replace javascript?
提问by william
My URLs
我的网址
http://www.mysite.com/folder1/page1.aspx
http://www.mysite.com/folder1/page1.aspx?id=1
http://www.mysite.com/folder1/page1.aspx?id=1&dt=20111128
Redirecting Page
重定向页面
http://www.mysite.com/folder1/page2.aspx
I want to redirect from page1.aspx
to page2.aspx
我想从重定向page1.aspx
到page2.aspx
How to write a javascript in page1.aspx
?
如何在 .js 中编写 javascript page1.aspx
?
window.location.replace("/page2.aspx");
window.location.replace("../page2.aspx");
window.location.replace("~/page2.aspx");
First 2 gave me this.
前2给了我这个。
http://www.mysite.com/page2.aspx
Last 1 gave me this.
最后1给了我这个。
http://www.mysite.com/folder1/~/page2.aspx
What is the correct way to use?
正确的使用方法是什么?
回答by T.J. Crowder
Include no path information at all, just like in a link:
完全不包含路径信息,就像在链接中一样:
window.location.replace("page2.aspx");
Here's a live exampleThe example switches between
这是一个活生生的例子该例子在
http://jsbin.com/asupup/2 -- The "2" corresponds to your "page1.aspx"
...and
...和
http://jsbin.com/asupup/3 -- The "3" corresponds to your "page2.aspx"
...and so the 2
page uses
...所以2
页面使用
window.location.replace("3");
...and the 3
page uses
...并且3
页面使用
window.location.replace("2");
For more about how URLs (and in particular relative URLs) work, see RFC3986. But basically:
有关 URL(尤其是相对 URL)如何工作的更多信息,请参阅RFC3986。但基本上:
If a relative URL doesn'tstart with
.
or/
, it replaces the last segment. So:http://foo.com/one/two/page.html + bar.html = http://foo.com/one/two/bar.html
If a relative URL starts with
../
, it replaces the last segment andthe one above it:http://foo.com/one/two/page.html + ../bar.html = http://foo.com/one/bar.html
Note that the
two
subfolder has been replaced. Multiple../
s can be used to move up multiple levels:http://foo.com/one/two/three/four/page.html + ../../bar.html = http://foo.com/one/two/bar.html
If a relative URL starts with a single
/
, it replaces everything after the hostname (and port, if any). So:http://foo.com/one/two/page.html + /bar.html = http://foo.com/bar.html http://foo.com:8080/one/two/page.html + /bar.html = http://foo.com:8080/bar.html
If a relative URL starts with
//
, it replaces everything following the protocol, so:http://ex.com/folder/page.html + //foo.com = http://foo.com
(This is handy when loading resources and you want to avoid worrying about
http
vs.https
and mixed-content warnings.)
如果相对 URL不以
.
或开头/
,它将替换最后一段。所以:http://foo.com/one/two/page.html + bar.html = http://foo.com/one/two/bar.html
如果相对 URL 以 开头
../
,它将替换最后一段和它上面的一段:http://foo.com/one/two/page.html + ../bar.html = http://foo.com/one/bar.html
请注意,
two
子文件夹已被替换。多个../
s 可用于向上移动多个级别:http://foo.com/one/two/three/four/page.html + ../../bar.html = http://foo.com/one/two/bar.html
如果相对 URL 以单个 开头
/
,它将替换主机名(和端口,如果有)之后的所有内容。所以:http://foo.com/one/two/page.html + /bar.html = http://foo.com/bar.html http://foo.com:8080/one/two/page.html + /bar.html = http://foo.com:8080/bar.html
如果相对 URL 以 开头
//
,它将替换遵循协议的所有内容,因此:http://ex.com/folder/page.html + //foo.com = http://foo.com
(这在加载资源时很方便,并且您希望避免担心
http
vs.https
和混合内容警告。)