13

Both document.getElementsByTagName('div') and document.querySelectorAll('div') return NodeList collection. The only difference is that first method returns live-collection and second one - a static one.

The question is - is there any opportunity to distinguish one object from another only via inspecting these objects (i.e - not trying to add/remove some items to test "liveness")?

shabunc
  • 23,119
  • 19
  • 77
  • 102

2 Answers2

6

The NodeList interface is agnostic of its dead or live status.

interface NodeList {
  Node item(in unsigned long index);
  readonly attribute unsigned long length;
};

It only contains a property length, and a method item so I'm afraid it's currently not possible to determine if an object is live without manipulating the DOM and seeing the effects.

Anurag
  • 140,337
  • 36
  • 221
  • 257
  • You're welcome, and a very interesting question. Is there a specific reason why you want to inspect the *liveness* of the object? If the reason is good and solves genuine problems, might as well propose it to the spec authors at w3. – Anurag Jul 27 '10 at 19:40
  • `document.querySelectorAll` returns a `StaticNodeList`, which by definition is not "live". Regardless of not being able to interrogate `NodeList`, I wonder if it's safe to assume `NodeList`, by convention, is *always* live. – Crescent Fresh Jul 28 '10 at 01:07
  • @Crescent - I didn't find a mention of `StaticNodeList` in [Selectors API level 2](http://www.w3.org/TR/2010/WD-selectors-api2-20100119/) or in [level 1](http://www.w3.org/TR/selectors-api/), but it is only mentioned in a [level 1 draft](http://www.w3.org/TR/2006/WD-selectors-api-20060926/). – Anurag Jul 28 '10 at 01:28
  • @Anurag: you are right. It appeared as late as Dec 07 (http://www.w3.org/TR/2007/WD-selectors-api-20071221/) but it looks like it has been replaced with `NodeList` as the return type. – Crescent Fresh Jul 28 '10 at 13:41
  • 3
    Still, the [latest version](http://www.w3.org/TR/selectors-api/#queryselectorall) states "The NodeList object returned by the querySelectorAll() method must be static, not live" - which is strange, since a NodeList is per se defined to be live. – user123444555621 Jul 28 '10 at 18:51
  • @Crescent - for example, in WebKit - document.querySelectorAll('div') instanceof NodeList is true, while trying to replace NodeList with StaticNodeList will cause an error - ReferenceError: Can't find variable: StaticNodeList – shabunc Jul 29 '10 at 07:12
  • 1
    @Anurag - well, actually I'have no any practical reason, but I do believe that such basic properties should be possible to inspect – shabunc Jul 29 '10 at 07:17
  • @shabunc: yeah, that would be evidence of `StaticNodeList` being no more :( Note however that a host environment (eg a browser) is not required (by spec) to expose *any* top-level types within the global scope. Eg it is not required that `NodeList` be a valid name in the browser either! – Crescent Fresh Jul 29 '10 at 19:07
6
a=document.querySelectorAll('a');
b=document.getElementsByTagName('a');

a.toString() == "[object NodeList]"
b.toString() == "[object HTMLCollection]"

(in FF/Chrome)

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
s79
  • 69
  • 1
  • 1
  • 1
    this is coincidental, not definitive. `document.getElementsByName('link').toString() == "[object NodeList]"` and it is live – chiliNUT Jan 27 '15 at 04:40