72

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.

Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
sikender
  • 5,883
  • 7
  • 42
  • 80

11 Answers11

115
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.

McKay
  • 12,334
  • 7
  • 53
  • 76
19
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 :)

robsch
  • 9,358
  • 9
  • 63
  • 104
Bogdan
  • 191
  • 1
  • 2
  • Simple... best approach in my opinion. I'm not using an iframe with a src attribute and so I'd rather not add a src attribute of "about:blank". I'm loading and clearing my iframe based on HTML data saved in a variable and so this was absolutely perfect. – Code Novice Apr 13 '21 at 18:25
  • Just fyi, this doesn't clear JS global scope. – luiscla27 Jul 28 '23 at 16:35
8

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).

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
3

Or you can do this :

var iframe_element = window.frames['iframe_name'];
iframe_element.document.open();
iframe_element.document.close();
Khalid
  • 31
  • 1
  • You can also write to the document while it is open, if you want to add a basic message (or what ever) to the window. – Trisped Aug 09 '12 at 01:04
2

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);

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
2

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>
Jared
  • 8,390
  • 5
  • 38
  • 43
2

// 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);
Keith
  • 49
  • 5
  • Just a note for others who are scanning for answers: This does not work if you have an applications with multiple iframes... some of which are not visible. Removing the `src` attribute does work though. – Lucretius May 24 '22 at 19:35
0

Just do it:

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

Work in Chrome

Mario Petrovic
  • 7,500
  • 14
  • 42
  • 62
-1

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);
Manas
  • 808
  • 1
  • 7
  • 16
-2
function getContentFromIframe(iFrameName)
{
    var myIFrame = document.getElementById(iFrameName);
    var content = myIFrame.contentWindow.document.body.innerHTML;

    //Do whatever you need with the content    
}
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
Frank James
  • 157
  • 2
  • 5
  • 15
-4

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

techknowfreak
  • 259
  • 4
  • 18
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Peter L. Aug 11 '13 at 07:04
  • You would also only be able to modify the contents of an iframe if it was originally loaded from the same domain as the containing page. – Tian van Heerden Nov 22 '15 at 09:04
  • 1
    @PeterL. Although I agree that this answer is too terse to be useful, it does, technically, answer the OP, since `jQuery` _is_ `JavaScript`. – Jesse Chisholm Jun 21 '16 at 15:51