php IFRAME中的PHP“标题(位置)”,加载到_top位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2985353/
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
PHP "header (location)" inside IFRAME, to load in _top location?
提问by Spoonk
I have a simple form which is inside IFRAME. When user click on SUBMIT, it redirects to a specific page on my server. The function I use for the redirect is
我有一个简单的表单,它在 IFRAME 里面。当用户单击提交时,它会重定向到我服务器上的特定页面。我用于重定向的功能是
 header ('Location: mypage2.html');
exit ();
But I want the new page to open in _top location, not inside the same IFRAME that I use. How can I tell the browser to open the new page in _top not inside the IFRAME? Thanks in advance.
但我希望新页面在 _top 位置打开,而不是在我使用的同一个 IFRAME 中。如何告诉浏览器在 _top 中而不是在 IFRAME 中打开新页面?提前致谢。
回答by Michal M
You are not able to achieve the desired effect in PHP. This is something you'd have to do from JavaScript or add targetattribute to <form>:
您无法在 PHP 中达到预期的效果。这是您必须从 JavaScript 执行的操作或将target属性添加到<form>:
<form ... target="_top">
回答by Dan Heberden
You can use javascript to access the parent. You could echo out javascript in your PHP.. so your parent page has this:
您可以使用 javascript 访问父级。你可以在你的 PHP 中回显 javascript .. 所以你的父页面有这个:
function changeURL( url ) {
    document.location = url;
}
and in your php script, you echo
在你的 php 脚本中,你回声
<script>
   parent.changeURL('mypage2.html' );
</script>
The reason you can't call parent.document.location is because it's read only - you have to have a function available on the parent to do it.
你不能调用 parent.document.location 的原因是它是只读的 - 你必须在父级上有一个可用的函数来做到这一点。
回答by Andrew
A simple way directly in PHP to redirect the parent page rather than the iframe:
直接在 PHP 中重定向父页面而不是 iframe 的简单方法:
echo "<script>top.window.location = '/mynewpage.php'</script>";
die;
The die;isn't necessarily necessary, but it is good "just in case" to prevent the script from continuing any further, for example, if javascript is disabled in the user's browser.
这die;不一定是必要的,但“以防万一”是很好的,以防止脚本继续运行,例如,如果在用户的浏览器中禁用了 javascript。
回答by bungdito
we can use javascript like this :
我们可以像这样使用javascript:
target.window.location='locationpage.html';
top.window.location='mypage2.html';
回答by Dimgba Kalu
The best and simplest thing to do is to use the form target element
最好和最简单的做法是使用表单目标元素
<form action="..." target="_top"> 
<form action="..." target="_parent">
either of the target parameters works fine
任何一个目标参数都可以正常工作
回答by Jas
You can either link to the page using <a href="pagename.php?Break=Y">Break Out</a>or use code 
您可以使用<a href="pagename.php?Break=Y">Break Out</a>或使用代码链接到页面
<?php header("Location: pagename.php?Break=Y"); ?>
You then use the following code in the header of the page with
然后在页面的标题中使用以下代码
if(isset($_GET['Break'])) // 
    {
    $BreakFrame = true;
    $BreakToPage = "pagename.php";
    }
<script language="JavaScript" type="text/javascript">
function changeURL( url ) {
    document.location = url;
}
</script>
<?php if($BreakFrame) { ?>
<script language="JavaScript" type="text/javascript">
parent.changeURL('<?=$BreakToPage?>' );
</script>
<? }?>
回答by Doberon
In the page that you want to load in _top, add the follow code in the header
在_top中要加载的页面,在header中添加如下代码
<script type="text/javascript">
if (top != self) top.location.href = location.href;
</script>

