Bug Tracker

Ticket #4136 (closed bug: fixed)

Opened 5 years ago

Last modified 5 years ago

Offset Bug in 1.3.1

Reported by: mike.helgeson Owned by: brandon
Priority: minor Milestone: 1.4
Component: offset Version: 1.3.1
Keywords: Cc:
Blocking: Blocked by:

Description

Passing any non-element into the offset method throws an error... I think it should instead return zero values...

$( document ).offset(); ERROR $( window ).offset(); ERROR

Attachments

offset_patch.js Download (2.6 KB) - added by mike.helgeson 5 years ago.

Change History

Changed 5 years ago by mike.helgeson

comment:1 Changed 5 years ago by john

  • Component changed from dimensions to offset
It looks like you added "
!this[0].ownerDocument" - doesn't seem too bad.

comment:2 Changed 5 years ago by thomasvsundert

I also experience this when trying to call position() (which invokes offset()) on an element within an IFrame. Please include the fix in the next version!

comment:3 Changed 5 years ago by thomasvsundert

Ok forget I said that, it's not true that the error occurs when it's called on elements within an IFrame. My error occured because I was trying to call $(undefined).position() and $(undefined) seems to return the HTML document, which then triggers the error.

comment:4 Changed 5 years ago by brandon

  • Status changed from new to closed
  • Resolution set to invalid
  • Milestone changed from 1.3.2 to 1.3.3

You can't run offset on window and when you run offset with no matched elements it should return null as it does now in SVN Trunk. This matches the behavior of other methods that return a result based on the first matched element (like .width()).

comment:5 Changed 5 years ago by mike.helgeson

  • Status changed from closed to reopened
  • Resolution invalid deleted

This function should return null. The issue is that is not happening in certain cases. Instead a javascript exception is thrown from:

$(window).offset() or $(document).offset()
TypeError: elem.ownerDocument is null

Maybe this is incorrect usage, but I feel like these calls should also return null, and not throw an error. Document and window are valid jquery object members, and don't cause errors in any other methods. Specifically, I have encountered this in my "drag" special event, I need to calculate the offset position... but I do not know if the event scope is going to be "document" or "window" and potentially break the code. I can test for document or window, but would rather have the "offset" and "position" methods test for "ownerDocument" The fix is quite simple...

Change:

if ( !elem ) return null;

To:

if ( !elem || !elem.ownerDocument ) return null;

comment:6 Changed 5 years ago by brandon

  • Status changed from reopened to closed
  • Resolution set to fixed

fixed in r6319

Note: See TracTickets for help on using tickets.