Javascript Iframe innerHTML

Does anyone know how to get the HTML out of an IFRAME I have tried several different ways:





Having something like the following would work.

<iframe id = "testframe" onload = populateIframe(;></iframe>

// The following function should be inside a script tag

function populateIframe(id) { 

    var text = "This is a Test"
var iframe = document.getElementById(id); 

var doc; 

if(iframe.contentDocument) { 
    doc = iframe.contentDocument; 
} else {
    doc = iframe.contentWindow.document; 

doc.body.innerHTML = text; 


Conroy's answer was right. In the case you need only stuff from body tag, just use:


You can get the source from another domain if you install the ForceCORS filter on Firefox. When you turn on this filter, it will bypass the security feature in the browser and your script will work even if you try to read another webpage. For example, you could open in an iframe and then read its source. The reason modern web brwosers deny this ability by default is because if the other domain includes a piece of JavaScript and you're reading that and displaying it on your page, it could contain malicious code and pose a security threat. So, whenever you're displaying data from another domain on your page, you must beware of this real threat and implement a way to filter out all JavaScript code from your text before you're going to display it. Remember, when a supposed piece of raw text contains some code enclosed within script tags, they won't show up when you display it on your page, nevertheless they will run! So, realize this is a threat.


You can get html out of an iframe using this code iframe = document.getElementById('frame'); innerHtml = iframe.contentDocument.documentElement.innerHTML


I think this is what you want:



I have it on good authority that this won't work in Chrome and Firefox although it works perfectly in IE, which is where I tested it. In retrospect, that was a big mistake

This will work:


I understand that this isn't exactly what was asked but don't want to delete the answer because I think it has a place.

I like @ravz's jquery answer below.


If you take a look at JQuery, you can do something like:

<iframe id="my_iframe" ...></iframe>


This is assuming that your iframe parent and child reside on the same server, due to the Same Origin Policy in Javascript.


Don't forget that you can not cross domains because of security.

So if this is the case, you should use JSON.


You can use the contentDocument or contentWindow property for that purpose. Here is the sample code.

function gethtml() {
  const x = document.getElementById("myframe")
  const y = x.contentWindow || x.contentDocument
  const z = y.document ? y.document : y

here, myframe is the id of your iframe. Note: You can't extract the content out of an iframe from a src outside you domain.


This solution works same as iFrame. I have created a PHP script that can get all the contents from the other website, and most important part is you can easily apply your custom jQuery to that external content. Please refer to the following script that can get all the contents from the other website and then you can apply your cusom jQuery/JS as well. This content can be used anywhere, inside any element or any page.

<div id='myframe'>

    Use below function to display final HTML inside this div

   //Display Frame
   echo displayFrame(); 



  Function to display frame from another domain 

function displayFrame()
  $webUrl = 'http://[]/';

  //Get HTML from the URL
  $content = file_get_contents($webUrl);

  //Add custom JS to returned HTML content
  $customJS = "

      /* Here I am writing a sample jQuery to hide the navigation menu
         You can write your own jQuery for this content
    //Hide Navigation bar


  //Append Custom JS with HTML
  $html = $content . $customJS;

  //Return customized HTML
  return $html;


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.