X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Foffset.js;h=b94faf37b200a612ac16626ed0cb3b53c7531fd0;hb=4fcfee4369d184d26ef819c34412fb8d9b09962c;hp=a3776c201172da4379878a57eb93740d011d7b50;hpb=9dc6e0c572b9c809a3a4c123071d96d48a01dd1c;p=jquery.git diff --git a/src/offset.js b/src/offset.js index a3776c2..b94faf3 100644 --- a/src/offset.js +++ b/src/offset.js @@ -5,7 +5,7 @@ var rtable = /^t(?:able|d|h)$/i, if ( "getBoundingClientRect" in document.documentElement ) { jQuery.fn.offset = function( options ) { - var elem = this[0]; + var elem = this[0], box; if ( options ) { return this.each(function( i ) { @@ -21,10 +21,19 @@ if ( "getBoundingClientRect" in document.documentElement ) { return jQuery.offset.bodyOffset( elem ); } - var box = elem.getBoundingClientRect(), - doc = elem.ownerDocument, - body = doc.body, - docElem = doc.documentElement, + try { + box = elem.getBoundingClientRect(); + } catch(e) {} + + var doc = elem.ownerDocument, + docElem = doc.documentElement; + + // Make sure we're not dealing with a disconnected DOM node + if ( !box || !jQuery.contains( docElem, elem ) ) { + return box || { top: 0, left: 0 }; + } + + var body = doc.body, win = getWindow(doc), clientTop = docElem.clientTop || body.clientTop || 0, clientLeft = docElem.clientLeft || body.clientLeft || 0, @@ -280,7 +289,7 @@ jQuery.each( ["Left", "Top"], function( i, name ) { }); function getWindow( elem ) { - return ("scrollTo" in elem && elem.document) ? + return jQuery.isWindow( elem ) ? elem : elem.nodeType === 9 ? elem.defaultView || elem.parentWindow :