Skip to main content

Bug Tracker

Side navigation

#6529 closed bug (fixed)

Opened May 05, 2010 09:50AM UTC

Closed September 24, 2010 07:53PM UTC

$("body").<somemethod>(...) throws if body does't exist

Reported by: hzr Owned by:
Priority: undecided Milestone: 1.4.3
Component: core Version: 1.4.2
Keywords: Cc:
Blocked by: Blocking:
Description

If $("body").<somemethod>(...) is used before the body element exists, it will throw.

E.g.:

<!doctype html>
<script src="jquery.js"></script>
<script>
$("body").click(function(){});
</script>
<p>Body

This will fail on line 1560 where it tries to read nodeType of null.

The bug seems to be because of the recent optimization for "body".

This should fix it (line 91):

  - if ( selector === "body" && !context ) {
  + if ( selector === "body" && !context && document.body ) {
Attachments (0)
Change History (4)

Changed May 06, 2010 12:03AM UTC by dmethvin comment:1

resolution: → invalid
status: newclosed

The body element should exist before any situation where it's needed. Please reopen with a specific test case.

Changed May 06, 2010 08:22AM UTC by hzr comment:2

resolution: invalid
status: closedreopened
The body element should exist before any situation where it's needed.

Agreed. But if it doesn't, the optimization for body shouldn't be done IMO, since it will result in an exception down the line.

Line 93 will set

this[0] = document.body;

even if

document.body
is
undefined
. This can easily be avoided by the proposed fix.

Please reopen with a specific test case.

There is a TC in the bug report. Note that I don't think there is a ''use-case'' for doing this, but if it's done, it shouldn't result in an exception.

Changed May 06, 2010 06:35PM UTC by dmethvin comment:3

Sorry, on second look I think you are absolutely right on all counts. If "body" doesn't (yet) exist, jQuery should return an empty set.

Changed September 24, 2010 07:53PM UTC by john comment:4

priority: → undecided
resolution: → fixed
status: reopenedclosed