簡體   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