Skip to main content

Bug Tracker

Side navigation

#11260 closed bug (invalid)

Opened January 31, 2012 06:31AM UTC

Closed January 31, 2012 08:15AM UTC

Last modified January 31, 2012 08:41AM UTC

Invoking contents() on a jQuery constructed iframe causes javascript error

Reported by: ts@gentics.com Owned by:
Priority: low Milestone: None
Component: manipulation Version: 1.7.1
Keywords: Cc:
Blocked by: Blocking:
Description
  • The version affected

jquery-1.7.1.js

  • The browsers you are able to reproduce the bug in

Google Chrome 16.0.912.77

Firefox 7.0.1

  • The operating system you experienced the bug on

Debian Squeeze

  • Step-by-step instructions on how to reproduce the issue

Execute the following code, for example in the Chrome console (in a page with jQuery loaded):

$("<iframe src='http://example.com'></iframe>").contents();

  • What you expect to happen, and what actually happens

I expect the expression above to execute without generating a javascript error. What happens is that a javascript error is logged in the console. This occurs for both Firfox and Chrome.

Example of Chrome error message:

Uncaught TypeError: Cannot read property 'document' of null

jQuery.each.contentsjquery-1.7.1.js:5515

jQuery.extend.mapjquery-1.7.1.js:771

jQuery.each.jQuery.fn.(anonymous function)jquery-1.7.1.js:5520

(anonymous function)

  • Information on other browsers where the issue does not occur

Only tested in Chrome and Firefox

Attachments (0)
Change History (3)

Changed January 31, 2012 08:15AM UTC by sindresorhus comment:1

component: unfiledmanipulation
priority: undecidedlow
resolution: → invalid
status: newclosed

The iframe needs to be inserted into the body and loaded before you can get it's contents.

Something like this: http://jsfiddle.net/mofle/M3pHT/

Also make sure it's on the same domain and subdomain.

In the future we would appreciate if you provided a testcase on jsfiddle when reporting bugs.

Changed January 31, 2012 08:31AM UTC by ts@gentics.com comment:2

I am aware that this only happens if the iframe is not inserted into the body. However, I thought this is an obvious and easy to correct bug in the jQuery code, so I thought I would report it, since javascript errors are always embarrassing.

I actually did make a test case on jsfiddle, but the ticket tracker would not let me submit the bugreport, always telling me that it is spam, until I figured that it may be caused by the short jsfiddle URL and removed it. Even after I created the ticket, the issue tracker would not let me add the jsfiddle URL as a comment. However is in charge of the Issue Tracke may want to have a look at this :-).

Changed January 31, 2012 08:41AM UTC by anonymous comment:3

By the way, this is not something I invented, it actually occurred in some production code I am maintaining! The code was iterating through a detached DOM tree, using contents() to get at the nodes on each level, which always failed when an iframe was encountered.