加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

如何使用window.location.replace javascript?

发布时间:2020-12-13 20:25:27 所属栏目:Windows 来源:网络整理
导读:我的网址 http://www.mysite.com/folder1/page1.aspxhttp://www.mysite.com/folder1/page1.aspx?id=1http://www.mysite.com/folder1/page1.aspx?id=1dt=20111128 重定向页面 http://www.mysite.com/folder1/page2.aspx 我想从page1.aspx重定向到page2.aspx
我的网址
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

重定向页面

http://www.mysite.com/folder1/page2.aspx

我想从page1.aspx重定向到page2.aspx

如何在page1.aspx中写一个javascript?

window.location.replace("/page2.aspx");
window.location.replace("../page2.aspx");
window.location.replace("~/page2.aspx");

第一个2给了我这个.

http://www.mysite.com/page2.aspx

最后一次给了我这个.

http://www.mysite.com/folder1/~/page2.aspx

什么是正确的使用方法?

包括没有路径信息,就像在链接中:
window.location.replace("page2.aspx");

Here’s a live example这个例子在之间切换

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");

…和3页使用

window.location.replace("2");

有关URL(特别是相对URL)如何工作的更多信息,请参阅RFC3986.但是基本上:

>如果相对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

请注意,这两个子文件夹已被替换.多个../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与https和混合内容警告.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读