PHP“标头(位置)”在 IFRAME 内部,加载到 _top 位置?

发布于 2024-09-04 20:47:40 字数 226 浏览 1 评论 0原文

我有一个简单的表单,位于 IFRAME 内。当用户单击“提交”时,它会重定向到我的服务器上的特定页面。我用于重定向的函数是

 header ('Location: mypage2.html');
exit ();

但我希望新页面在 _top 位置打开,而不是在我使用的同一 IFRAME 内。如何告诉浏览器在 _top 中而不是 IFRAME 中打开新页面? 提前致谢。

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

 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

罗罗贝儿 2024-09-11 20:47:40

在 PHP 中你无法达到预期的效果。这是您必须通过 JavaScript 执行的操作,或者将 target 属性添加到

中:

<form ... target="_top">

You are not able to achieve the desired effect in PHP. This is something you'd have to do from JavaScript or add target attribute to <form>:

<form ... target="_top">
爱格式化 2024-09-11 20:47:40

直接在 PHP 中重定向父页面而不是 iframe 的简单方法:

echo "<script>top.window.location = '/mynewpage.php'</script>";
die;

die; 不一定是必需的,但“以防万一”可以很好地防止脚本继续继续,例如,如果用户浏览器中禁用了 javascript。

A simple way directly in PHP to redirect the parent page rather than the 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.

真心难拥有 2024-09-11 20:47:40

您可以使用 javascript 访问父级。你可以在你的PHP中回显javascript..所以你的父页面有这个:

function changeURL( url ) {
    document.location = url;
}

在你的php脚本中,你回显

<script>
   parent.changeURL('mypage2.html' );
</script>

你不能调用parent.document.location的原因是因为它是只读的 - 你必须有一个可用的函数交给家长去做。

You can use javascript to access the parent. You could echo out javascript in your PHP.. so your parent page has this:

function changeURL( url ) {
    document.location = url;
}

and in your php script, you echo

<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.

∞觅青森が 2024-09-11 20:47:40

我们可以像这样使用 JavaScript:

target.window.location='locationpage.html';
top.window.location='mypage2.html';

we can use javascript like this :

target.window.location='locationpage.html';
top.window.location='mypage2.html';
百善笑为先 2024-09-11 20:47:40

最好和最简单的做法是使用表单目标元素,

<form action="..." target="_top"> 
<form action="..." target="_parent">

任何一个目标参数都可以正常工作

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

菊凝晚露 2024-09-11 20:47:40

您可以使用 Break Out 链接到该页面,也可以使用代码

<?php header("Location: pagename.php?Break=Y"); ?>

您然后在 header 中使用以下代码页面与

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>

<? }?>

You can either link to the page using <a href="pagename.php?Break=Y">Break Out</a> or use code

<?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>

<? }?>
甜妞爱困 2024-09-11 20:47:40

在_top中要加载的页面中,在标题中添加以下代码

<script type="text/javascript">
if (top != self) top.location.href = location.href;
</script>

In the page that you want to load in _top, add the follow code in the header

<script type="text/javascript">
if (top != self) top.location.href = location.href;
</script>
奈何桥上唱咆哮 2024-09-11 20:47:40

对于 CakePHP 4,要重定向您的父页面,只需在 iframe 的链接中添加选项 'target'=>'_top'

示例:

 <?= $this->Html->link(__('Redirect Parent'), ['controller' => 'Users', 'action' => 'view'], ['target'=>'_top']) ?>

祝一切顺利!

For CakePHP 4, to redirect your parent page just add option 'target'=>'_top' in your iframe's link:

Example:

 <?= $this->Html->link(__('Redirect Parent'), ['controller' => 'Users', 'action' => 'view'], ['target'=>'_top']) ?>

All the best!

时光匆匆的小流年 2024-09-11 20:47:40

您可以向重定向添加多个标头。
我使用了一个我为此创建的小函数。

public function redirect($url){
   header('Window-target: _top');
   header('Location:' . $url, true, 303);
   exit();
}

You can add multiple headers to your redirect.
I use a little function that I created with this in mind.

public function redirect($url){
   header('Window-target: _top');
   header('Location:' . $url, true, 303);
   exit();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文