7

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>
3
  • 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. Commented Oct 20, 2011 at 13:41
  • @Tetaxa, how to check the domain for iframe? It is very possible that they come from different domain. Commented Oct 20, 2011 at 13:46
  • the first part of the url. if the pages are on the same server you should be alright. Commented Oct 20, 2011 at 13:48

3 Answers 3

6

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/>
Sign up to request clarification or add additional context in comments.

6 Comments

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?
Yes. Anyway if the page is from the same server, then my code works. Please see update about same origin
Still got doc undefine, very strange.
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.
It IS a security issue if the protocol, the domain or the port is different.
|
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.

4 Comments

I suppose this should work. But I got contDoc undefined after I run your 2 statements. The returned iframe seems to be the right one.
@Thomson: It will work unless the iframe source is set to another domain.
I'm getting Uncaught DOMException: Blocked a frame with origin "example.com" from accessing a cross-origin frame. any hint?
@oneway: If you own the iframe code, check workaround: stackoverflow.com/a/25098153/750216
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.