Javascript Iframe innerHTML

Asked
Active3 hr before
Viewed126 times

6 Answers

iframejavascript
90%

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, Is there a way to make an air conditioner without venting heat outdoors? ,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 think this is what you want:

window.frames['iframe01'].document.body.innerHTML

This will work:

window.frames[0].document.body.innerHTML
load more v
88%

getIframeContent(frameId): It is used to get the object reference of an iframe.,contentWindow: It is a property which returns the window object of the iframe.,contentWindow.document: It returns the document object of iframe window.,contentWindow.document.body.innerHTML: It returns the HTML content of iframe body.

Code snippet:

function getIframeContent(frameId) {
   var frameObj =
      document.getElementById(frameId);

   var frameContent = frameObj.
   contentWindow.document.body.innerHTML;

   alert("frame content : " + frameContent);
}
load more v
72%

Is it possible for the parent of an iFRAME to extract the HTML of the iFRAME’s source document? I stumbled across a code faq that stated this was the way to do it:,…I get the HTML of the parent instead of the iFRAME content that I wish to reference. I have also tried:,I am trying to capture as a string (with which i can do parsing & comparisons) the HTML (not the rendered content) of an I-FRAME(not a standard frame)'s content.,Is this possible to do? and if so what is the proper path?

Is it possible for the parent of an iFRAME to extract the HTML of the iFRAME’s source document? I stumbled across a code faq that stated this was the way to do it:

var content = document.getElementById('myIFrame').document.innerHTML;

when I add the body reference like so:

var content = document.getElementById('myIFrame').document.body.innerHTML;

…I get the HTML of the parent instead of the iFRAME content that I wish to reference. I have also tried:

var content = document.getElementById('myIFrame').document.documentElement.innerHTML;

and

var content = document.getElementById('myIFrame').documentElement.innerHTML;
load more v
65%

Does anyone know how to get the HTML out of an IFRAME I have tried several different ways:,   JavaScript  -  iframe  -  innerhtml ,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 is assuming that your iframe parent and child reside on the same server, due to the Same Origin Policy in Javascript.

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

document.getElementById('iframe01').contentDocument.body.innerHTML
document.frames['iframe01'].document.body.innerHTML
document.getElementById('iframe01').contentWindow.document.body.innerHTML
load more v
75%

Another method consists of setting the src property to javascript:"&nbsp;", and register an one-time load event handler. This method is slightly more complex, hence I do not describe it in a deeper detail.,In Firefox, the frame's content seems to not be recognised when no initial content has been set. The easiest method to solve this, is shown in the code below:,document.documentElement refers to the root element of a document, usually <html>. It's possible that the body property of document isn't ready yet, when you call your current code. By referring to the root element, this problem is circumvented.,You're setting the innerHTML while there's an in-flight about:blank load for the iframe. When that finishes loading, it replaces the document you modified.

I've looked for a cross-browser way to programmatically set the innerHTML of an IFrame. (As in http://roneiv.wordpress.com/2008/01/18/get-the-content-of-an-iframe-in-javascript-crossbrowser-solution-for-both-ie-and-firefox/). I've written some sample code (below), and I can get it to work in Safari and Chrome, but not Firefox. Can anyone help me figure out what I need to do instead (in a portable way)?

<html>

<body>

   <div name=FRAME2div id=FRAME2div style="position:absolute; background-color:blue; color:black; border-color:black;border-width:0; left:100px; top:40px; width:200px; height:200px; overflow:scroll; display:block; ">
   </div>

   <script type="text/javascript">
      document.getElementById("FRAME2div").innerHTML = '<iframe border=0 id=IFRAME2 name=IFRAME2 ></iframe>';
      document.getElementById("IFRAME2").contentWindow.document.body.innerHTML = '<html><body><p>NETSCAPE</p></body></html>';
   </script>

</body>

</html>
load more v
40%

How can I get the contents of an external iframe in IE,This would allow you to insert content from another page in an iframe.,How can I check if the iframe loading has gone well ?,I want to get that raw HTML code that is in the IFrame.

I came up with the following function that seems to do the job in both Firefox (tested on version 2.0.0.11 and 3.03 ) and in IE (6 and 7):

function getContentFromIframe(iFrameName) {

   var myIFrame = document.getElementById(iFrameName);
   var content = myIFrame.contentWindow.document.body.innerHTML;

   //Do whatever you need with the content    

}
load more v

Other "iframe-javascript" queries related to "Javascript Iframe innerHTML"