From: Thomas 'PointedEars' Lahn on
Matt Kruse wrote:

> function containsId1(el,id) {
> return (el.ownerDocument.evaluate('descendant-or-self::*[@id = "' +id
> + '"]', el, null, 9, null).singleNodeValue !== null);
> }
> function containsId2(el,id) {
> return (el.querySelectorAll('#'+id).length==1);
> }
> function containsId3(el,id) {
> var o = document.getElementById(id);
> if (o) {
> if (id===o.id) { return true; }
> while (o=o.parentNode) {
> if (id===o.id) { return true; }
> }
> }
> return false;
> }
>
> It turns out that the XPath approach is about 100ms slower over 5,000
> iterations of a simple test case (insignificant), and the other two
> are practically equal. So I guess there is no reason not to use the
> last approach, which is the most backwards-compatible.

Told you :)


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
From: Matt Kruse on
On May 27, 12:52 pm, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote:
> No, to determine if #x is a child of the inserted element, the simplest
> approach is to compare the inserted element to x's parentNode.
> To determine if #x is a *descendant* of the inserted element is a
> different task. It is not a difficult one; and many recent browsers
> supply functionality

Yes, I meant descendant, not direct child.

> You wrote that you are using a DOMNodeInserted mutation event. Perhaps
> that is not the best choice here.
> What are you trying to do?

I'm actually writing script to manipulate Facebook. When objects with
certain id's are injected into the page, I want to manipulate them.
Since Facebook's navigation scheme is a mess, catching node insertions
and seeing if a given id has been inserted works well.

Matt Kruse
From: Garrett Smith on
On 5/27/2010 11:21 AM, Matt Kruse wrote:
> On May 27, 12:52 pm, Garrett Smith<dhtmlkitc...(a)gmail.com> wrote:
>> No, to determine if #x is a child of the inserted element, the simplest
>> approach is to compare the inserted element to x's parentNode.
>> To determine if #x is a *descendant* of the inserted element is a
>> different task. It is not a difficult one; and many recent browsers
>> supply functionality
>
> Yes, I meant descendant, not direct child.
>
>> You wrote that you are using a DOMNodeInserted mutation event. Perhaps
>> that is not the best choice here.
>> What are you trying to do?
>
> I'm actually writing script to manipulate Facebook. When objects with
> certain id's are injected into the page, I want to manipulate them.
> Since Facebook's navigation scheme is a mess, catching node insertions
> and seeing if a given id has been inserted works well.
>

I'm still a little fuzzy on the big picture, so this may not help.

Check to see if #x exists prior to appending the container. If it does,
the container not contain #x (unless the container has removed #x and
appends it to itself at that time).

Otherwise, after appending the container, check to see if #x exists. If
it does, then the container contains it.

var x = document.getElementById("x");
addContainer();
containerHasX = !x && document.getElementById("x");

Mutation events have been known to hurt performance. I'm not up on the
details of which mutation events hurt performance in which browsers.
Generally I avoid mutation event because they don't work in IE <= 8.

Garrett
From: Garrett Smith on
On 5/27/2010 12:17 PM, Garrett Smith wrote:
> On 5/27/2010 11:21 AM, Matt Kruse wrote:
>> On May 27, 12:52 pm, Garrett Smith<dhtmlkitc...(a)gmail.com> wrote:

[...]

> var x = document.getElementById("x");
> addContainer();
> containerHasX = !x && document.getElementById("x");
>

Make that:

containerHasX = !x && document.getElementById("x") !== null;

Garrett
From: Thomas 'PointedEars' Lahn on
Garrett Smith wrote:

> On 5/27/2010 11:21 AM, Matt Kruse wrote:
>> On May 27, 12:52 pm, Garrett Smith<dhtmlkitc...(a)gmail.com> wrote:
>>> You wrote that you are using a DOMNodeInserted mutation event. Perhaps
>>> that is not the best choice here.
>>> What are you trying to do?
>>
>> I'm actually writing script to manipulate Facebook. When objects with
>> certain id's are injected into the page, I want to manipulate them.
>> Since Facebook's navigation scheme is a mess, catching node insertions
>> and seeing if a given id has been inserted works well.
>
> I'm still a little fuzzy on the big picture, so this may not help.
>
> Check to see if #x exists prior to appending the container. If it does,
> the container not contain #x (unless the container has removed #x and
> appends it to itself at that time).

YGCIB.

> [...]
> Mutation events have been known to hurt performance. I'm not up on the
> details of which mutation events hurt performance in which browsers.
> Generally I avoid mutation event because they don't work in IE <= 8.

Polling the status of the document tree very likely hurts performance a lot
more than listening to mutation events. Granted, polling is more
compatible, but there is no good reason to do that where mutation events are
supported. And you don't seem to have noticed that the problem has already
been solved satisfactorily.


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(a)news.demon.co.uk> (2004)