繁体   English   中英

使用 iframe 清除网页上的内容

[英]Clear content on web page with iframes

我想使用 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/ (然后点击提交按钮)。 在“建议”部分之后,我希望它将用户重定向到网站的主页。

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

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

这个 plunker 中查看它的运行情况 单击(在 iframe 内)这两个按钮以查看它们如何以不同方式加载页面。

笔记:

根据该属性的MDN 文档,示例中的<script>标记中的语言属性在示例中被替换为type属性:

MDN 脚本页面截图 1


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM