我的网址
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
Run Code Online (Sandbox Code Playgroud)
重定向页面
http://www.mysite.com/folder1/page2.aspx
Run Code Online (Sandbox Code Playgroud)
我想重定向page1.aspx
到page2.aspx
如何写一个javascript page1.aspx
?
window.location.replace("/page2.aspx");
window.location.replace("../page2.aspx");
window.location.replace("~/page2.aspx");
Run Code Online (Sandbox Code Playgroud)
前2给了我这个.
http://www.mysite.com/page2.aspx
Run Code Online (Sandbox Code Playgroud)
最后1给了我这个.
http://www.mysite.com/folder1/~/page2.aspx
Run Code Online (Sandbox Code Playgroud)
什么是正确的使用方法?
T.J*_*der 54
根本不包含路径信息,就像在链接中一样:
window.location.replace("page2.aspx");
Run Code Online (Sandbox Code Playgroud)
这是一个实例.示例在两者之间切换
http://jsbin.com/asupup/2 -- The "2" corresponds to your "page1.aspx"
...和
http://jsbin.com/asupup/3 -- The "3" corresponds to your "page2.aspx"
...所以2
页面使用
window.location.replace("3");
Run Code Online (Sandbox Code Playgroud)
...并且3
页面使用
window.location.replace("2");
Run Code Online (Sandbox Code Playgroud)
有关URL(尤其是相对URL)如何工作的更多信息,请参阅RFC3986.但基本上:
如果相对URL 不以.
或开头/
,则替换最后一个段.所以:
http://foo.com/one/two/page.html
+ bar.html
= http://foo.com/one/two/bar.html
Run Code Online (Sandbox Code Playgroud)如果相对URL以...开头../
,它将替换最后一个段及其上方的段:
http://foo.com/one/two/page.html
+ ../bar.html
= http://foo.com/one/bar.html
Run Code Online (Sandbox Code Playgroud)
请注意,two
子文件夹已被替换.多个../
s可用于向上移动多个级别:
http://foo.com/one/two/three/four/page.html
+ ../../bar.html
= http://foo.com/one/two/bar.html
Run Code Online (Sandbox Code Playgroud)如果相对URL以单个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
Run Code Online (Sandbox Code Playgroud)如果相对URL开头//
,它将替换协议后面的所有内容,因此:
http://ex.com/folder/page.html
+ //foo.com
= http://foo.com
Run Code Online (Sandbox Code Playgroud)
(这在加载资源时非常方便,并且您希望避免担心http
vs https
和混合内容警告.)
归档时间: |
|
查看次数: |
59765 次 |
最近记录: |