Bug Tracker

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#11260 closed bug (invalid)

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

Reported by: ts@… 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

Change History (3)

comment:1 Changed 7 years ago by sindresorhus

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.

comment:2 Changed 7 years ago by ts@…

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 :-).

comment:3 Changed 7 years ago by anonymous

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.

Note: See TracTickets for help on using tickets.