How to get whole content of iframe?

by edmond_brakus , in category: Javascript , 8 months ago

How to get whole content of iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by deron , 8 months ago

@edmond_brakus 

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:
1
<iframe id="myIframe" src="https://www.example.com"></iframe>


  1. Use JavaScript to retrieve the whole content of the iframe:
1
2
3
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.