C# 从 IFrame 重定向父页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2092275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect the parent page from IFrame
提问by Waheed
I am using an IFrame, and from this IFrame I want to redirect to another page.
我正在使用 IFrame,我想从这个 IFrame 重定向到另一个页面。
Please tell me how to do this without any JavaScript, ie, no window.location
.
请告诉我如何在没有任何 JavaScript 的情况下执行此操作,即没有window.location
.
Response.Redirect
shows the page in the IFrame, but I want to show page as a main page.
Response.Redirect
在 IFrame 中显示页面,但我想将页面显示为主页。
采纳答案by o.k.w
It will be a hazard if we can manipulate other frames/window withou using client-side scripts or user-invoked actions.
如果我们可以在不使用客户端脚本或用户调用的操作的情况下操作其他框架/窗口,那将是一种危险。
Here's a list of alternatives:
以下是备选方案列表:
Javascript options:
Javascript 选项:
window.top.location.href=theLocation;
window.parent.location.href=theLocation;
window.top.location.replace(theLocation);
Non-javascript options:
非 JavaScript 选项:
<a href="theLocation" target="_top">Click here to continue</a>
<a href="theLocation" target="_parent">Click here to continue</a>
回答by Michal Dymel
I think there is no way to do it without JS. Browser will treat every redirect from server in the iframe. You have to 'tell' it to reload whole window using JavaScript.
我认为没有JS就没有办法做到这一点。浏览器将在 iframe 中处理来自服务器的每个重定向。您必须“告诉”它使用 JavaScript 重新加载整个窗口。
回答by gingerbreadboy
has to be javascript as far as i know.
据我所知,必须是 javascript。
self.parent.location='http://'
回答by Waheed
I used this code.
我使用了这个代码。
ClientScript.RegisterStartupScript(GetType(), "Load", "<script type='text/javascript'>window.parent.location.href = '../CentinelError.aspx'; </script>");
And it works.
它有效。
回答by Synox
Well, this is really a hack, but you could define Parent-Frame as default target:
好吧,这确实是一个 hack,但是您可以将 Parent-Frame 定义为默认目标:
<base target="_parent">
As this will apply to all your links in the iframe, this may not be a satisfying solution ;-)
由于这将适用于 iframe 中的所有链接,因此这可能不是一个令人满意的解决方案 ;-)
回答by Charles Prakash Dasari
Like all others have pointed out, you can't do it without using JavaScript. However, on the server side you can emit the necessary JavaScript so that the page gets redirected to the target location as soon as it loads within the iframe.
就像其他所有人都指出的那样,不使用 JavaScript 就无法做到这一点。但是,在服务器端,您可以发出必要的 JavaScript,以便页面在 iframe 中加载后立即重定向到目标位置。
回答by user336735
you CAN do this without javascript, if you have access to the head block of the remote page:
如果您有权访问远程页面的 head 块,则无需 javascript 即可执行此操作:
<base target="_parent" />
very simple, easy, 1-line solution, if you have access to remote page head. no javascript.
非常简单,容易,1 行解决方案,如果您可以访问远程页头。没有javascript。
回答by user3351873
Response.Write( "<script>window.open('" + url + "','_parent');</script>" );
Answered in the below link http://forums.asp.net/t/1349064.aspx?Redirect+parent+page+from+within+Iframe
在以下链接中回答 http://forums.asp.net/t/1349064.aspx?Redirect+parent+page+from+within+Iframe
回答by user3351873
We can redirect from both server and client side when using Iframe<>
使用 Iframe<> 时,我们可以从服务器端和客户端重定向
Client side response:
客户端响应:
window.parent.location.href="http://yoursite.com"
Server side response:
服务器端响应:
Response.Write("<script type=text/javascript> window.parent.location.href ='http://yoursite.com' </script>")