Sometimes, we want to clear the content of an iframe with JavaScript.
In this article, we’ll look at how to clear the content of an iframe with JavaScript.
How to clear the content of an iframe with JavaScript?
To clear the content of an iframe with JavaScript, we call write
to write an empty string.
For instance, we write
const iframe = document.getElementById("myiframe");
const html = "";
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(html);
iframe.contentWindow.document.close();
to select the iframe with getElementById
.
Then we call open
to open the iframe’s document.
Next, we call write
to clear the iframe’s document.
And then we call close
to close it.
Conclusion
To clear the content of an iframe with JavaScript, we call write
to write an empty string.