window.location.href的⽤法
javascript中的location.href有很多种⽤法,主要如下。
self.location.href="/url" 当前页⾯打开URL页⾯
location.href="/url" 当前页⾯打开URL页⾯
windows.location.href="/url" 当前页⾯打开URL页⾯,前⾯三个⽤法相同。
this.location.href="/url" 当前页⾯打开URL页⾯
parent.location.href="/url" 在⽗页⾯打开新页⾯
top.location.href="/url" 在顶层页⾯打开新页⾯
如果页⾯中⾃定义了frame,那么可将parent self top换为⾃定义frame的名称,效果是在frame窗⼝打开url地址
此外,window.location.href=window.location.href;和 window.location.Reload()和都是刷新当前页⾯。区别在于是否有提交数据。当有提交数据时,window.location.Reload()会提⽰是否提交,window.locatio
n.href=window.location.href;则是向指定的url提交数据
在写ASP.Net程序的时候,我们经常遇到跳转页⾯的问题,我们经常使⽤Response.Redirect 做ASP.NET框架页跳转,如果客户要在跳转的时候使⽤提⽰,这个就不灵光了,如:
复制代码代码如下:
Response.Write("< script>alert('恭喜您,注册成功!');< /script>");
Response.Redirect("main.html");
这时候我们的提⽰内容没有出来就跳转了,和Response.Redirect("main.html");没有任何区别。
这时我们采⽤下⾯代码试验⼀下:
ASP.NET框架页跳转的另⼀实现
复制代码代码如下:
Response.Write("< script language=javascript>alert('恭喜您,注册成功!')< /script>");
Response.Write("< script language=javascript>window.location.href='main.html'< /script>");
这个即实现了我们的要求,在提⽰后,跳转页⾯。
最重要的是window.location.href 语句可以实现⼀个框架的页⾯在执⾏服务器端代码后刷新另⼀个框架的页⾯(Response.Redirect⽆法达到,⾄少我没有发现):
如:index.htm页⾯中有⼆个框架,分别为 frameLeft和frameRight,在frameRight页⾯中执⾏服务器端代码后刷新frameLeft中的页⾯。
先前最常见的是注册之后,⾃动刷新登陆框,让登陆框换成已登陆页⾯,只要在注册成功的代码之后加上⼀段,即可以实现刷新另个框架的页⾯。代码如下:
复制代码代码如下:
Response.Write("< script language=javascript>alert('恭喜您,注册成功!')< /script>");
Response.Write("< script language=javascript>window.parent.frameLeft.location.href='main.html'< /script>");
这样就搞定了ASP.NET框架页跳转中断的问题。其实asp、php中⼀般都使⽤这种⽅式。
"window.location.href"、"location.href"是本页⾯跳转
"parent.location.href"是上⼀层页⾯跳转
"top.location.href"是最外层的页⾯跳转
举例说明:
如果A,B,C,D都是jsp,D是C的iframe,C是B的iframe,B是A的iframe,如果D中js这样写html frame
"window.location.href"、"location.href":D页⾯跳转
"parent.location.href":C页⾯跳转
"top.location.href":A页⾯跳转
如果D页⾯中有form的话,
<form>: form提交后D页⾯跳转
<form target="_blank">: form提交后弹出新页⾯
<form target="_parent">: form提交后C页⾯跳转
<form target="_top"> : form提交后A页⾯跳转
关于页⾯刷新,D 页⾯中这样写:
"load();": C页⾯刷新(当然,也可以使⽤⼦窗⼝的 opener 对象来获得⽗窗⼝的对象:window.opener.load(); )
"load();": A页⾯刷新

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。