Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to distinguish between live and non-live NodeList collections?

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")?

thanks in advance

like image 592
shabunc Avatar asked Jul 26 '10 21:07

shabunc


2 Answers

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.

like image 127
Anurag Avatar answered Nov 02 '22 04:11

Anurag


a=document.querySelectorAll('a');
b=document.getElementsByTagName('a');

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

(in FF/Chrome)

like image 20
s79 Avatar answered Nov 02 '22 04:11

s79