How to clear the content of an IFRAME?

前端 未结 10 2409
感情败类
感情败类 2020-12-14 05:17

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 = \"bl

相关标签:
10条回答
  • 2020-12-14 05:48
    function getContentFromIframe(iFrameName)
    {
        var myIFrame = document.getElementById(iFrameName);
        var content = myIFrame.contentWindow.document.body.innerHTML;
    
        //Do whatever you need with the content    
    }
    
    0 讨论(0)
  • 2020-12-14 05:49

    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>
    
    0 讨论(0)
  • 2020-12-14 05:49

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

    0 讨论(0)
  • 2020-12-14 05:51

    // 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);
    
    0 讨论(0)
  • 2020-12-14 05:52

    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);
    
    0 讨论(0)
  • 2020-12-14 06:02
    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 :)

    0 讨论(0)
提交回复
热议问题