To be honest, John, I haven't tested this implementation using frames... if
you say yes, it works, then I think we could stick with it... but wouldn't
it be a little (just a little) faster to get "doc" outside the method call,
in an outter scope, to maximize performance? It seems to me that it was one
of the points that lowered 30% the time for processing the call, in my
benchmarks...

Again, thanks for everyone for the support; I'm really happy for how sane
and healthy is the discussion in here... and for everyone being receptive to
me... :-)

Diogo



On Tue, Dec 2, 2008 at 2:25 PM, John Resig <[EMAIL PROTECTED]> wrote:

>
> Why not just tweak this implementation to be relative? That way you
> still get performance but also allow it to work across frames, etc.
>
> $.fn.inDOM = document.documentElement.contains ?
>       function() {
>           var el = this[0], doc = el.documentElement;
>           return doc !== el && doc.contains(el);
>        } :
>       function() {
>           var el = this[0];
>            return !!(el.documentElement.compareDocumentPosition(el) & 16);
>       };
>
> --John
>
>
>
> On Tue, Dec 2, 2008 at 9:41 AM, Ariel Flesler <[EMAIL PROTECTED]> wrote:
> >
> > I'd just save it as a local var
> >
> > var html = document.documentElement;
> > $.fn.inDOM = html.contains ?
> >        function() {
> >            var el = this[0];
> >            return html != el && $.html.contains(el);
> >        } :
> >        function() {
> >            var el = this[0];
> >            return !!(html.compareDocumentPosition(el) & 16);
> >        };
> >
> > Does this work reliably on any browser ?
> > Note that his doesn't work for any document that is not THE document.
> >
> > I think a simple traversal (going up) would do. I'm sorry to spoil all
> > the researching but no one said this function will be used everywhere
> > and it needs to be as fast as possible. First it most be effective,
> > then efficient.
> >
> > On Tue, Dec 2, 2008 at 12:07 PM, Diogo Baeder <[EMAIL PROTECTED]>
> wrote:
> >> Thanks!
> >>
> >> Guys, the last proposal gave me about 140ms ~ 150ms within 1000 method
> >> calls, but here's something that gave me about 80ms ~ 90ms:
> >>
> >>
> >>     $.htmlEl = $('html').get(0);
> >>     $.fn.inDOM = $.htmlEl.contains ?
> >>         function() {
> >>             var el = this[0];
> >>             return $.htmlEl != el && $.htmlEl.contains(el);
> >>         } :
> >>         function() {
> >>             var el = this[0];
> >>             return !!($.htmlEl.compareDocumentPosition(el) & 16);
> >>         };
> >>
> >> What do you think of it? A little bit more of coding, but considerably
> >> faster... despite someone would rarely make so much calls to this
> method, I
> >> think...
> >>
> >> Diogo
> >>
> >>
> >>
> >> On Mon, Dec 1, 2008 at 10:36 PM, Diego Perini <[EMAIL PROTECTED]>
> >> wrote:
> >>>
> >>> Diogo,
> >>>
> >>> On 1 Dic, 21:56, "Diogo Baeder" <[EMAIL PROTECTED]> wrote:
> >>> > Thank you, guys... I tried the last method as implemented/adapted by
> >>> > Diego,
> >>> > and it works! :-)
> >>> >
> >>> > Unfortunately, neither "contains" nor "compareDocumentPosition" are
> >>> > being
> >>> > recognized as methods for the "document" node in IE6 or 7, as you can
> >>> > test
> >>> > yourselves:
> >>> >
> >>> > alert(document.contains);
> >>> > alert(window.contains);
> >>> > alert(document.compareDocumentPosition);
> >>> > alert(window.compareDocumentPosition);
> >>> >
> >>> > Any ideas why these didn't work for me?
> >>> >
> >>>
> >>> The Microsoft "contains" method does not exists for the document
> >>> object (at least in the docs). Maybe because it is not an element
> >>> (nodeType == 1).
> >>>
> >>> Firefox and Opera implement the "compareDocumentPosition" method, also
> >>> they made it a method of the "document" itself.
> >>>
> >>> Opera implements both "compareDocumentPosition" and "contains", Safari
> >>> only has "contains" again not on the "document".
> >>>
> >>> The conclusion is that the implementations of "compareDocumentPosition
> >>> ()" and "contains()" disagree on this specific fact.
> >>>
> >>> At this point, given the messed up implementations, better being
> >>> independent from them both and go for the traversal.
> >>>
> >>> It is also much shorter and really cross-browser...
> >>>
> >>> --
> >>> Diego
> >>>
> >>>
> >>> > Diogo
> >>> >
> >>> > On Mon, Dec 1, 2008 at 10:58 AM, Diego Perini
> >>> > <[EMAIL PROTECTED]>wrote:
> >>> >
> >>> >
> >>> >
> >>> >
> >>> >
> >>> > > ricardo,
> >>> >
> >>> > > On 1 Dic, 05:35, ricardobeat <[EMAIL PROTECTED]> wrote:
> >>> > > > I always miss the obvious stuff :)
> >>> >
> >>> > > > I ended up with this, it's a lot faster than Diogo's code but
> still
> >>> > > > requires you to traverse the document all the way up. John
> Resig's
> >>> > > > 'contains' function is at least twice faster in FF though, so it
> >>> > > > seems
> >>> > > > like the perfect solution.
> >>> >
> >>> > > > $.fn.inDOM = function(){
> >>> > > >    var el = this[0];
> >>> > > >    while (el.parentNode) el = el.parentNode;
> >>> > > >    return el == this[0].ownerDocument;
> >>> >
> >>> > > > };
> >>> >
> >>> > > To make it less dependent:
> >>> >
> >>> > > $.fn.inDOM = function(){
> >>> > >   var el = this[0];
> >>> > >   while (el.parentNode) el = el.parentNode;
> >>> > >    return el.nodeType == 9;
> >>> > > };
> >>> >
> >>> > > now you can also pass elements present in other DOM contexts.
> >>> >
> >>> > > Don't know if this modifies the intended usage, this will most
> >>> > > generally tell if the element is an orphan (not attached to any
> >>> > > document).
> >>> >
> >>> > > To account for both usages, it will be necessary to pass context
> >>> > > information (an extra parameter to specify the desired context).
> >>> >
> >>> > > --
> >>> > > Diego
> >>> >
> >>> > > > - ricardo
> >>> >
> >>> > > > On 30 nov, 19:43, "Ariel Flesler" <[EMAIL PROTECTED]> wrote:
> >>> >
> >>> > > > > Yeah, parentNode is not good enough.
> >>> >
> >>> > > > > On Sun, Nov 30, 2008 at 4:35 PM, ricardobeat
> >>> > > > > <[EMAIL PROTECTED]>
> >>> > > wrote:
> >>> >
> >>> > > > > > Hi,
> >>> >
> >>> > > > > > It seems the ownerDocument is set for the created element
> even
> >>> > > > > > if
> >>> > > it's
> >>> > > > > > not in the DOM, it's the document where jQuery was loaded in.
> A
> >>> > > simple
> >>> > > > > > check for parentNode or offsetParent would do:
> >>> >
> >>> > > > > > $.fn.inDOM = function(){
> >>> > > > > >     return !!this.parentNode; //boolean
> >>> > > > > > });
> >>> >
> >>> > > > > > parentNode returns faster for elements in the DOM, while
> >>> > > > > > offsetParent
> >>> > > > > > returns faster for elements not in the DOM (in FF3 at least).
> >>> >
> >>> > > > > > Hope I'm not missing anything. It surely would need a better
> >>> > > > > > name :]
> >>> >
> >>> > > > > > cheers,
> >>> > > > > > - ricardo
> >>> > > > > > On 29 nov, 14:46, Ariel Flesler <[EMAIL PROTECTED]> wrote:
> >>> > > > > >> 'body' should be replaced by 'html'.
> >>> >
> >>> > > > > >> Maybe we can make it faster by consulting expandos like
> >>> > > > > >> ownerDocument ?
> >>> >
> >>> > > > > >> --
> >>> > > > > >> Ariel Fleslerhttp://flesler.blogspot.com
> >>> >
> >>> > > > > >> On Nov 27, 12:52 am, diogobaeder <[EMAIL PROTECTED]>
> wrote:
> >>> >
> >>> > > > > >> > Hi there,
> >>> >
> >>> > > > > >> > I'm new here (and in jQuery), but even though I'd like to
> >>> > > > > >> > propose
> >>> > > some
> >>> > > > > >> > simple but usefull method to the jQuery object (at
> core.js)
> >>> > > > > >> > to
> >>> > > tell
> >>> > > > > >> > the API user if an element exists in the document. I've
> tried
> >>> > > > > >> > to
> >>> > > build
> >>> > > > > >> > one as follows:
> >>> >
> >>> > > > > >> > [CODE]
> >>> > > > > >> > (function($) {
> >>> >
> >>> > > > > >> >     $.fn.inDOM = function() {
> >>> > > > > >> >         return !!this.parents('body').length;
> >>> > > > > >> >     };
> >>> >
> >>> > > > > >> > })(jQuery);
> >>> >
> >>> > > > > >> > jQuery(document).ready(function(){
> >>> > > > > >> >     var jEl = $('.someExistingClass');
> >>> >
> >>> > > > > >> >     // Should be in DOM
> >>> > > > > >> >     console.debug(jEl.inDOM());
> >>> >
> >>> > > > > >> >     // Removing the element
> >>> > > > > >> >     jEl.remove();
> >>> >
> >>> > > > > >> >     // Should NOT be in DOM
> >>> > > > > >> >     console.debug(jEl.inDOM());});
> >>> >
> >>> > > > > >> > [/CODE]
> >>> >
> >>> > > > > >> > So, if the client sets a variable as a jQuery object, and
> at
> >>> > > > > >> > some
> >>> > > > > >> > point of the code the DOM element within it can be
> removed,
> >>> > > > > >> > he/she
> >>> > > can
> >>> > > > > >> > test if it really was. OK, I know it sounds unsignificant,
> >>> > > > > >> > but I
> >>> > > think
> >>> > > > > >> > it would still be usefull.
> >>> >
> >>> > > > > >> > Thanks!
> >>> >
> >>> > > > > >> > Diogo Baeder
> >>> >
> >>> > > > > --
> >>> > > > > Ariel Fleslerhttp://flesler.blogspot.com
> >>> >
> >>> > --
> >>> > Diogo Baederhttp://www.diogobaeder.com.br
> >>>
> >>
> >>
> >>
> >> --
> >> Diogo Baeder
> >> http://www.diogobaeder.com.br
> >>
> >> >
> >>
> >
> >
> >
> > --
> > Ariel Flesler
> > http://flesler.blogspot.com
> >
> > >
> >
>
> >
>


-- 
Diogo Baeder
http://www.diogobaeder.com.br

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to