简体   繁体   English

清除现有的iframe内容

[英]clear existing iframe contents

I have a list of files. 我有一个文件列表。 When i click on them, the contents are displayed inside an frame. 当我点击它们时,内容显示在一个框架内。 The problem i have is: when i open one file and move to next one (if the file is of greater size, it takes some time to load), the contents of old file still shows up in iframe. 我遇到的问题是:当我打开一个文件并移动到下一个文件时(如果文件大小更大,加载需要一些时间),旧文件的内容仍会显示在iframe中。 As soon as i click next file, i want to clear the contents of old file in iframe and meanwhile it's loading i can display a loading indicator. 一旦我点击下一个文件,我想清除iframe中旧文件的内容,同时它正在加载我可以显示一个加载指示器。

To clear the contents inside iframe i tried some ways but could not succeed. 要清除iframe中的内容,我尝试了一些方法,但无法成功。 Can you advise me on this 你能告诉我这个吗?

My HTML 我的HTML

<div id="result">
 <div style="overflow: auto;" id="testIframeBox">
 <iframe id="testiframe" name="testIframe" onload="">iFramenot supported</iframe></div>
</div>

I show up the clicked file using location.replace 我使用location.replace显示单击的文件

window.frames['testIframe'].location.replace(URL+"&download=1&inline=1&resourceId="+resourceId+"&time=" + Date.now());

To clear contents: 清除内容:

if(document.getElementById('testiframe'))
    {
    var frame = document.getElementById("testIframe"),
    frameDoc = frame.contentDocument || frame.contentWindow.document;
    frameDoc.documentElement.innerHTML="";
    }

Also i tried to replace the contents with loading indicator before filling the contents: 此外,我尝试在填充内容之前用加载指示器替换内容:

window.frames['testIframe'].location.replace('div class="loading-animation"></div>');

As I understand, there are two parts to your question: 据我了解,您的问题分为两部分:

  1. ... To clear the contents inside iframe ... ...清除iframe内的内容......

The answer to this is simple, you use the src attribute of the iframe to control its contents. 答案很简单,您使用iframesrc属性来控制其内容。 Commonly accepted practice is to assign a about:blank to the src to cause it to load browsers's default blank document. 通常接受的做法是为src分配一个about:blank ,使其加载浏览器的默认空白文档。

contentDocument is supposed to be used only when the document which you are loading in your iframe is in the same domain, otherwise cross-domain security blocks your attempts. 仅当您在iframe中加载的文档位于同一域中时,才应使用contentDocument ,否则跨域安全性会阻止您的尝试。

  1. ..I tried to replace the contents with loading indicator before filling the contents.. ..在填充内容之前,我试图用加载指示器替换内容。

This is tricky. 这很棘手。 load event on iframe does fire consistently across browsers, but this does not mean the document is ready. iframe上的load事件会在浏览器中一致地触发,但这并不意味着文档已准备就绪。 DOMContentLoaded is flaky in its implementation across browsers. DOMContentLoaded在跨浏览器的实现方面很不稳定。 Of the browsers I use, at least Chrome fails to fire it. 在我使用的浏览器中,至少Chrome无法启动它。 DOMFrameContentLoaded event is moz specific and only Firefox fires it. DOMFrameContentLoaded事件moz特殊,只火狐激发它。

See this: How to properly receive the DOMContentLoaded event from an XUL iframe? 请参阅: 如何从XUL iframe正确接收DOMContentLoaded事件?

One option with you is to use jQuery and rely on its on.("load") handler which may provide you with near-consistent results. 您可以选择使用jQuery并依赖其on.("load")处理程序,它可以为您提供接近一致的结果。 But, as I see from your question that you are not using jQuery and relying on plain-vanilla Javascript. 但是,正如我从你的问题中看到你没有使用jQuery并依赖于普通的Javascript。 Here, IMO the cheapest option for you is to handle the load event and inject a faux delay using setTimeout to simulate loading. 在这里,IMO最便宜的选择是处理load事件并使用setTimeout注入faux延迟来模拟加载。 The following snippet will make it clear to you. 以下代码段将向您说明。

Snippet : 片段

 document.getElementById("a1").addEventListener("click", load); document.getElementById("a2").addEventListener("click", load); document.getElementById("testiframe").addEventListener("load", loaded); function load() { var lnk = this.innerText; var iframe = document.getElementById("testiframe"); iframe.src = "about:blank"; document.getElementById("loader").style.display = "block"; setTimeout(function() { iframe.src = lnk; /* small delay to avoid successive src assignment problems */ }, 500); } function loaded() { /* faux delay to allow for content loading */ setTimeout(function() { document.getElementById("loader").style.display = "none"; }, 1000); } 
 div#testIframeBox { width: 320px; height: 240px; position: relative; } iframe { width: 100%; height: 100%; position: absolute; top: 0px; left: 0px; } div#testIframeBox > div { position: absolute; top: 16px; left: 16px; padding: 8px; background-color: yellow; display: none; } 
 <div> <a href="#" id="a1">http://jquery.com</a> &nbsp;|&nbsp; <a href="#" id="a2">http://example.com</a> </div> <hr /> <div id="result"> <div id="testIframeBox"> <iframe id="testiframe" name="testIframe"></iframe> <div id="loader">Loading...</div> </div> </div> 

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

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