【问题标题】:Clear content on web page with iframes使用 iframe 清除网页上的内容
【发布时间】:2017-03-01 17:42:33
【问题描述】:

我想使用 Javascript 或 PHP 清除网页。

但是,我相信页面的一个区域是:data-form="manual iframe"

因此,当我尝试“清除”页面时,它只会清除该 iframe 中的数据。剩下的内容还在。

有谁知道如何清除页面上的所有内容,而不仅仅是 iframe 中的内容?

我的总体目标是清除当前网页中的所有内容,然后将用户重定向到完全不同的网页(主页)。

这是我目前的代码(在 PHP 中):

// An attempt using javascript (didn't work, only cleared info in iframe)
echo "<script language=\"javascript\">

var body = document.getElementById('body');
body.innerHTML ='';

var divrow1 = document.getElementByClassName('row');
divrow1.innerHTML ='';
</script>
";

// Another attempt using PHP (didn't work only cleared content in iframe)
document.write ("");

//This is where I want page to redirect to:
header("Location: http://www.challengehut.com/index.php");
exit;

如果您想查看整个内容,请从这里开始: 点击这里:http://challengehut.com/TheChallenges/ (然后点击提交按钮)。在“建议”部分之后,我希望它将用户重定向到网站的主页。

【问题讨论】:

标签: javascript php redirect iframe


【解决方案1】:

在 iframe 的脚本中,您可以检查代码是否在 iframe 内,使用 window.parent:

if (parent.window != window) {
    //inside iframe
}

然后在此基础上,可以相应地设置位置:

if (parent.window != window) {
    parent.window.location = 'http://www.challengehut.com/index.php';
}

因此,对于 PHP 代码,您可能只需要在脚本标记中(在简单的 html 页面中)呈现它,而不是使用 header() 函数:

<?
echo '<html><body><script type="text/javascript">
        if (parent.window != window) {
             parent.window.location = "http://www.challengehut.com/index.php";
         }
         window.location = "http://www.challengehut.com/index.php";
</script></body></html>';
?>

this plunker 中查看它的实际应用。单击(在 iframe 内)这两个按钮以查看它们如何以不同方式加载页面。

注意:

您的示例 &lt;script&gt; 标记中的 language 属性在示例中被替换为 type 属性,给定该属性的 MDN documentation

1


1https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#Attributes

【讨论】:

    猜你喜欢
    • 2010-12-19
    • 2014-11-07
    • 1970-01-01
    • 2020-10-11
    • 2012-07-19
    • 1970-01-01
    • 2016-02-12
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多