2017-03-01 41 views
0

我想使用Javascript或PHP清除网页。使用iframes清除网页上的内容

不过,我相信页面的一个区域是: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/ (然后点击提交按钮)。在“建议”部分之后,我希望它将用户重定向到网站的主页。

+0

参见[如何清除的iFrame的从另一个的iFrame的内容](http://stackoverflow.com/questions/33645685/how-to-clear-the-contents-of-an -iframe-from-another-iframe/33649349?s = 1 | 1.1516#33649349) – guest271314

+0

谢谢。我的主要目标是将用户重定向到新页面。我希望这个新的页面能够填满整个网页(不只是小型的iFrame)。有没有简单的方法来做到这一点? – Andrea

+0

@Andrea你看到我的答案了吗?如果您有任何问题,请告诉我。 –

回答

0

在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内部)这两个按钮来查看它们以不同方式加载页面的方式。

注:

语言属性在你的榜样<script>标签是与属性的例子取代,鉴于MDN documentation该属性:

screenshot from MDN script page 1


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