0

I am writing a browser extension/add-in that traverses the complete html tree of the webpage. Essentially, I get the children of each node using node.childNodes and then call the traversing function recursively. This worked great until I came across an iframe. Even if the iFrame has content, node.childNodes always shows the iframe as having 0 children.

I have seen examples of how to handle this (for example http://pietschsoft.com/post/2004/08/12/JavaScript-How-to-get-value-from-nested-form-in-iframe.aspx), but these examples assume that I know the name or id of the iframe. However, the iframe might not have a name or id, just a src.

user984003
  • 28,050
  • 64
  • 189
  • 285
  • It looks like this is a cross domain issue. This may help : http://stackoverflow.com/questions/729577/can-javascript-access-iframe-elements-from-the-parent-page – Adrien Schuler Mar 28 '12 at 14:58

2 Answers2

2

You can identify that a node is an iframe by checking if node.tagName == "IFRAME".

If it is, then instead of looking for node.childNodes, you would look at node.contentDocument.documentElement.

Should be fairly simple.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • And it was :) I did: element.contentDocument.documentElement.childNodes – user984003 Mar 28 '12 at 15:09
  • Ah, but it doesn't always work. I sometimes get that node.contentDocument is undefined. I see from James Hill's answer that it isn't always possible to get the contents of an iframe? I'm not sure why it's a security "same origin issue" since I am only trying to READ html that has already been sent to my browser. I can, after all, see it when I click "Inspect element" in Chrome. – user984003 Mar 28 '12 at 18:46
  • The Inspector is trusted by the browser (since, well, it *is* the browser). Your code, however, is not. So you can't access the contents of an iframe if said iframe contains content from another domain. – Niet the Dark Absol Mar 28 '12 at 19:00
0

If you want to traverse all of the elements within the iFrame, then you need to access the document object of the iFrame. Pseudocode:

if(currentElement == iframe) {
    TraverseDOM(currentElement.contentDocument.documentElement);
}

Note: If the content of the iFrame is originates in a different domain, you will not be able to access it's contents. See Same Origin Policy.

James Hill
  • 60,353
  • 20
  • 145
  • 161