How to get the body’s content of an iframe in JavaScript?

Spread the love

Sometimes, we want to get the body’s content of an iframe in JavaScript.

In this article, we’ll look at how to get the body’s content of an iframe in JavaScript.

How to get the body’s content of an iframe in JavaScript?

To get the body’s content of an iframe in JavaScript, we use the contentDocument property.

For instance, we write

const iframe = document.querySelector("#id_description_iframe");
const iframeDocument = iframe.contentDocument ?? iframe.contentWindow.document;

to select the iframe with querySelector.

Then we use contentDocument or contentWindow.document to get the iframe’s body’s content.

Conclusion

To get the body’s content of an iframe in JavaScript, we use the contentDocument property.

Leave a Reply

Your email address will not be published.