How to get the element inside an iframe element in Javascript?

17,332

Solution 1

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/>

Solution 2

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.

Share:
17,332
Thomson
Author by

Thomson

Human debugger.

Updated on June 18, 2022

Comments

  • Thomson
    Thomson almost 2 years

    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
    Thomson over 12 years
    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
    Thomson over 12 years
    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?
  • mplungjan
    mplungjan over 12 years
    Yes. Anyway if the page is from the same server, then my code works. Please see update about same origin
  • Thomson
    Thomson over 12 years
    Still got doc undefine, very strange.
  • Thomson
    Thomson over 12 years
    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.
  • mplungjan
    mplungjan over 12 years
    It IS a security issue if the protocol, the domain or the port is different.
  • Răzvan Flavius Panda
    Răzvan Flavius Panda over 12 years
    @Thomson: It will work unless the iframe source is set to another domain.
  • mplungjan
    mplungjan over 12 years
    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
  • rnrneverdies
    rnrneverdies almost 9 years
    I'm getting Uncaught DOMException: Blocked a frame with origin "example.com" from accessing a cross-origin frame. any hint?
  • Răzvan Flavius Panda
    Răzvan Flavius Panda almost 9 years
    @oneway: If you own the iframe code, check workaround: stackoverflow.com/a/25098153/750216