How to get whole content of iframe?

by edmond_brakus , in category: Javascript , 7 months ago

How to get whole content of iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer


by deron , 7 months ago


To get the whole content of an iframe using JavaScript, you can use the contentWindow property of the iframe element and then access the document property of the content window. You can then retrieve the content of the iframe as follows:

  1. Identify the iframe element by its id or class:
<iframe id="myIframe" src=""></iframe>

  1. Use JavaScript to retrieve the whole content of the iframe:
var iframe = document.getElementById('myIframe'); // acquire the iframe element
var iframeContent = iframe.contentWindow.document.documentElement.outerHTML; // retrieve the content
console.log(iframeContent); // display the whole content in the browser console

The resulting iframeContent variable will contain the entire HTML content of the iframe.