Bug Tracker

Opened 13 years ago

Closed 13 years ago

#6529 closed bug (fixed)

$("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 ) {

Change History (4)

comment:1 Changed 13 years ago by dmethvin

Resolution: invalid
Status: newclosed

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

comment:2 Changed 13 years ago by hzr

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.

comment:3 Changed 13 years ago by dmethvin

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.

comment:4 Changed 13 years ago by john

Priority: undecided
Resolution: fixed
Status: reopenedclosed
Note: See TracTickets for help on using tickets.