[转载]window.location.href的用法(动态输出跳转)

不管在静态页面仍是动态输出页面中window.location.href都是不错的用了跳转的实现方案
 

javascript中的location.href有不少种用法,主要以下。javascript

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" 在顶层页面打开新页面php

若是页面中自定义了frame,那么可将parent self top换为自定义frame的名称,效果是在frame窗口打开url地址html

此外,window.location.href=window.location.href;和 window.location.Reload()和都是刷新当前页面。区别在因而否有提交数据。当有提交数据 时,window.location.Reload()会提示是否提 交,window.location.href=window.location.href;则是向指定的url提交数据
java

在写ASP.Net程序的时候,咱们常常遇到跳转页面的问题,咱们常常使用Response.Redirect 作ASP.NET框架页跳转,若是客户要在跳转的时候使用提示,这个就不灵光了,如:windows

复制代码 代码以下:

Response.Write("< script>alert('恭喜您,注册成功!');< /script>"); 
Response.Redirect("main.html");  

这时候咱们的提示内容没有出来就跳转了,和Response.Redirect("main.html");没有任何区别。服务器

这时咱们采用下面代码试验一下:框架

ASP.NET框架页跳转的另外一实现jsp

复制代码 代码以下:

Response.Write("< script language=javascript>alert('恭喜您,注册成功!')< /script>"); 
Response.Write("< script language=javascript>window.location.href='main.html'< /script>");

这个即实现了咱们的要求,在提示后,跳转页面。this

最重要的是window.location.href 语句能够实现一个框架的页面在执行服务器端代码后刷新另外一个框架的页面(Response.Redirect没法达到,至少我没有发现):url

如: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这样写

"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 页面中这样写:

"parent.location.reload();": C页面刷新 (固然,也可使用子窗口的 opener 对象来得到父窗口的对象:window.opener.document.location.reload(); )

"top.location.reload();": A页面刷新

相关文章
相关标签/搜索