0

I'm having a problem getting Chrome to return the contents of an iframe using jQuery contents(). The following code works as expected in FireFox, but Chrome doesn't return any contents of the body. The iframe document is in the same domain as the main document and does indeed have some content. (I include the iframe twice in the document, distinguishing each using .eq(#) selector which is why jQuery.fn.init[2] shows 2 elements.) As I noted, this works in FireFox.

var $iframe_contents = $("iframe").contents();

excerpt from Chrome debugger.

$iframe_contents: jQuery.fn.init[2]  (There are 2 iframes in the document)
0: document
   URL: "about:blank"
   activeElement: body
   all: HTMLAllCollection[3]
   ...
   baseURI: "about:blank"
   ...
   **body: body**
   ...
   baseURI: "about:blank"
   ...
   **childElementCount: 0
   childNodes: NodeList[0]
   children: HTMLCollection[0]**
   classList: DOMTokenList[0]
   ...
   dataset: DOMStringMap
   dir: ""
   ...
  **firstChild: null
  firstElementChild: null**
  hidden: false
  id: ""
  **innerHTML: ""
  innerText: ""**
  ...
  **lastChild: null
  lastElementChild: null**
  link: ""
  localName: "body"
  .....
  .....
ProGM
  • 6,949
  • 4
  • 33
  • 52
Irwin
  • 11
  • 2
  • `$("iframe").contents()` returns the document object of the iframe so since you are getting 2 objects that seems to be correct.... can you try `$("iframe").contents().find('body').css('background-color', 'red')` or something – Arun P Johny Mar 18 '15 at 01:00
  • @Arun - Thanks, but that's not the problem. The objects returned are empty, e.g., children: HTMLCollection[0]. My actual code does have a find() method which, in this situation, returns null. This all works fine in Firefox. – Irwin Mar 18 '15 at 18:56
  • I doubt you could use HTMLCollection[0] directly..Check this on why that wont work http://stackoverflow.com/a/22754453/909535 – meteor Mar 19 '15 at 18:18

1 Answers1

1

I believe I've resolved my problem. It appears that Firefox works differently than the other browsers. It loads the iframes and then triggers .ready() whereas Chrome, Safari and IE trigger .ready() before the iframe is present in the DOM. So for the others, wait until the iframe is loaded before initializing the feature modules.

Irwin
  • 11
  • 2