【发布时间】:2011-01-06 17:30:10
【问题描述】:
我正在使用 IFrame,我想从这个 IFrame 重定向到另一个页面。
请告诉我如何在没有任何 JavaScript 的情况下执行此操作,即没有 window.location。
Response.Redirect 在 IFrame 中显示页面,但我想将页面显示为主页。
【问题讨论】:
-
没有JS是不行的。
我正在使用 IFrame,我想从这个 IFrame 重定向到另一个页面。
请告诉我如何在没有任何 JavaScript 的情况下执行此操作,即没有 window.location。
Response.Redirect 在 IFrame 中显示页面,但我想将页面显示为主页。
【问题讨论】:
如果我们可以在不使用客户端脚本或用户调用的操作的情况下操纵其他框架/窗口,那将是一种危险。
以下是替代品列表:
Javascript 选项:
window.top.location.href=theLocation;
window.parent.location.href=theLocation;
window.top.location.replace(theLocation);
非 JavaScript 选项:
<a href="theLocation" target="_top">Click here to continue</a>
<a href="theLocation" target="_parent">Click here to continue</a>
【讨论】:
我使用了这个代码。
ClientScript.RegisterStartupScript(GetType(), "Load", "<script type='text/javascript'>window.parent.location.href = '../CentinelError.aspx'; </script>");
而且它有效。
【讨论】:
使用 iframe 时,我们可以从服务器端和客户端进行重定向
客户端响应:
window.parent.location.href="http://yoursite.com"
服务器端响应:
Response.Write("<script type=text/javascript> window.parent.location.href ='http://yoursite.com' </script>")
【讨论】:
我认为没有 JS 就没有办法做到这一点。浏览器将处理 iframe 中来自服务器的每个重定向。您必须“告诉”它使用 JavaScript 重新加载整个窗口。
【讨论】:
嗯,这确实是一个 hack,但您可以将 Parent-Frame 定义为默认目标:
<base target="_parent">
由于这将适用于 iframe 中的所有链接,因此这可能不是一个令人满意的解决方案 ;-)
【讨论】:
据我所知必须是 javascript。
self.parent.location='http://'
【讨论】:
就像所有其他人指出的那样,如果不使用 JavaScript,您就无法做到这一点。但是,在服务器端,您可以发出必要的 JavaScript,以便页面在 iframe 中加载后立即重定向到目标位置。
【讨论】:
如果您可以访问远程页面的头块,则无需 javascript 即可执行此操作:
<base target="_parent" />
如果您可以访问远程页眉,则非常简单、容易、1 行的解决方案。没有javascript。
【讨论】:
Response.Write( "<script>window.open('" + url + "','_parent');</script>" );
在下面的链接中回答 http://forums.asp.net/t/1349064.aspx?Redirect+parent+page+from+within+Iframe
【讨论】: