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
function getContentFromIframe(iFrameName)
{
var myIFrame = document.getElementById(iFrameName);
var content = myIFrame.contentWindow.document.body.innerHTML;
//Do whatever you need with the content
}
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>
$("#iframe").contents().find("body").html('');
// 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);
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);
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 :)