How to clear the content of an IFRAME?

JavascriptHtmlIframe

Javascript Problem Overview


How do I clear the content of my IFRAME element, using javascript, without loading a blank page into it?

I can figure out to do this: iframe_element.src = "blank.html", but there must be a better, instant, method.

Javascript Solutions


Solution 1 - Javascript

about:blank

is a "URL" that is blank. It's always clear

You can set the page's source to that, and it will clear.

Solution 2 - Javascript

var iframe = document.getElementById("myiframe");
var html = "";

iframe.contentWindow.document.open();
iframe.contentWindow.document.write(html);
iframe.contentWindow.document.close();

tested in IE, Firefox and Chrome ... it did it :)

Solution 3 - Javascript

Your technique is the most robust. Its the one I use myself. At times content may be delivered over HTTPS and the use of about:blank can cause warning messages to appear to the effect of "do you want to include content from unsecure location" or some such thing.

Something being instant is a matter of perception however if you have a single Blank.html file on your site configured with a long cache expiry the client will only ever fetch the page once (at the most once per-session).

Solution 4 - Javascript

Or you can do this :

var iframe_element = window.frames['iframe_name'];
iframe_element.document.open();
iframe_element.document.close();

Solution 5 - Javascript

I have had difficulties with "about:blank" on pages with many IFrames. It does not seem to be a valid location in every browser (I never found out for sure, though). Anyway, I am happy with javascript:void(0);

Solution 6 - Javascript

You could also do this:

<html>
<head>
<script>
    var doc = null;
    window.onload = function() {
        alert("Filling IFrame");
        doc = document.getElementById("test");
		
        if( doc.document ) {
            document.test.document.body.innerHTML = "<h1>test</h1>"; //Chrome, IE
        }else {
            doc.contentDocument.body.innerHTML = "<h1>test</h1>"; //FireFox
        }
		
            setTimeout(function() { 
                alert("Clearing IFrame");

                if( doc.document ) {
                    document.test.document.body.innerHTML = ""; //Chrome, IE
                }else {
                    doc.contentDocument.body.innerHTML = ""; //FireFox
                }
	
            }, 1000);
        };
    </script>
</head>

<body>
    <iframe id="test" name="test">

    </iframe>
</body>
</html>

Solution 7 - Javascript

// First I get the window from its Id.

var my_content = document.getElementById('my_iframe').contentWindow.document;

// Then I clear it by setting the body tag inner HTML to an empty string.

my_content.body.innerHTML="";

// Now when I write my new content it does not get appended to the end of the body and the iframe body will contain only fresh content.

my_content.write(new_content);

Solution 8 - Javascript

Just do it:

var iframe = document.getElementById("iframe");
iframe.removeAttribute('srcdoc');

Work in Chrome

Solution 9 - Javascript

Just get the Iframe and remove the documentElement from it. The Iframe will be blank

 var frame = document.getElementById("YourFrameId"),
 frameDoc = frame.contentDocument || frame.contentWindow.document;
 frameDoc.removeChild(frameDoc.documentElement);

Solution 10 - Javascript

function getContentFromIframe(iFrameName)
{
    var myIFrame = document.getElementById(iFrameName);
    var content = myIFrame.contentWindow.document.body.innerHTML;

    //Do whatever you need with the content    
}

Solution 11 - Javascript

$("#iframe").contents().find("body").html('');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsikenderView Question on Stackoverflow
Solution 1 - JavascriptMcKayView Answer on Stackoverflow
Solution 2 - JavascriptBogdanView Answer on Stackoverflow
Solution 3 - JavascriptAnthonyWJonesView Answer on Stackoverflow
Solution 4 - JavascriptKhalidView Answer on Stackoverflow
Solution 5 - JavascriptPekkaView Answer on Stackoverflow
Solution 6 - JavascriptJaredView Answer on Stackoverflow
Solution 7 - JavascriptKeithView Answer on Stackoverflow
Solution 8 - JavascriptIsaque SimiaoView Answer on Stackoverflow
Solution 9 - JavascriptManasView Answer on Stackoverflow
Solution 10 - JavascriptFrank JamesView Answer on Stackoverflow
Solution 11 - JavascripttechknowfreakView Answer on Stackoverflow