6

I have a element as below inside the document, I could get the iframe element by document.getElementById('iframe_id'), but how to get the element inside this iframe? I tried iframeElement.contentWindow, and the returned DOMWindow has no properties. Also tried iframeElement.docuemnt and iframeElement.contentDocument, both of them are undefined. How can I get it? I am using the latest Chrome in my experiment.

Here is the iframe element

<iframe id='iframe_id'>
<html>
<body>many content here</body>
</html>
</iframe>
Thomson
  • 18,073
  • 19
  • 75
  • 125
  • 2
    are your page and the page inside the iframe from the same domain? if not, chrome will probably not give you access to the other DOM. – Tetaxa Oct 20 '11 at 13:41
  • @Tetaxa, how to check the domain for iframe? It is very possible that they come from different domain. – Thomson Oct 20 '11 at 13:46
  • the first part of the url. if the pages are on the same server you should be alright. – Tetaxa Oct 20 '11 at 13:48

3 Answers3

5

You can ONLY interrogate content in an iframe if the content has the same protocol, domain and port number as the script that interrogates it. It is called SAME ORIGIN

If that is the case, then this code will show the content. If not - you cannot access the iframe from a normal script in a normal html page

Demo - tested in IE8, Chrome 13 and Fx6

function showIframeContent(id) {
  var iframe = document.getElementById(id);
    try {
      var doc = (iframe.contentDocument)? iframe.contentDocument: iframe.contentWindow.document;
      alert(doc.body.innerHTML);
    }
    catch(e) {
       alert(e.message);
    }
  return false;
}


<iframe id='iframe_id1' src="javascript:parent.somehtml()"> </iframe>
<br/>
<a href="#" onclick="return showIframeContent('iframe_id1')">Show</a>
<hr/>

<iframe id='iframe_id2' src="http://plungjan.name/"> </iframe>
<br/>
<a href="#" onclick="return showIframeContent('iframe_id2')">Show</a>
<hr/>
mplungjan
  • 134,906
  • 25
  • 152
  • 209
  • The above sample html is just excerpt (simplified) from the Elements window at the bottom of Chrome. It shows that there is a full html inside the iframe. Do you mean this is fake in the UI? – Thomson Oct 20 '11 at 13:41
  • Yes. Anyway if the page is from the same server, then my code works. Please see update about same origin – mplungjan Oct 20 '11 at 13:43
  • Still got `doc` undefine, very strange. – Thomson Oct 20 '11 at 13:44
  • I just run your demo in my Chrome, and the the message box shows the right content in inside the html. So it could be some security issue that Chrome blocks to access the inner DOM since it comes from a different domain, as Tetaxa mentioned in the comment. – Thomson Oct 20 '11 at 13:54
  • It IS a security issue if the protocol, the domain or the port is different. – mplungjan Oct 20 '11 at 14:15
  • I have updated the fiddle with code that might tell you where the iframe content comes from - it is crude but should give you an idea – mplungjan Oct 20 '11 at 14:33
4

Having:

var iframe = document.getElementById('iframe_id');

To get the content document you can use:

var contDoc = iframe.contentDocument || iframe.contentWindow.document;

Then you can search for your element inside the iframe by id.

Răzvan Flavius Panda
  • 20,376
  • 13
  • 101
  • 153
0

You should be able to access it by document.getElementById('yourIFrame').document.getElementById('yourElement')

dwalldorf
  • 1,381
  • 3
  • 11
  • 21