I'm not sure my current implementation is available all the time:
function isNodeList(nodes) { var result = Object.prototype.toString.call(nodes); // modern browser such as IE9 / firefox / chrome etc. if (result === '[object HTMLCollection]' || result === '[object NodeList]') { return true; } //ie 6/7/8 if (typeof(nodes) != 'object') { return false; } // detect length and item if (!('length' in nodes) || !('item' in nodes)) { return false; } // use the trick NodeList(index),all browsers support try { if (nodes(0) === null || (nodes(0) && nodes(0).tagName)) return true; } catch (e) { return false; } return false; }
A common situation is {length:1,item:function(){return [];}}
The value of result in chrome / safari / opera is '[object NodeList]'.
In firefox and IE 9 , it is '[object HTMLCollection]'.
Which is the standard value?
A NodeList and an HTMLcollection is very much the same thing. Both are array-like collections (lists) of nodes (elements) extracted from a document.
A NodeList is a collection of document nodes (element nodes, attribute nodes, and text nodes). HTMLCollection items can be accessed by their name, id, or index number. NodeList items can only be accessed by their index number. An HTMLCollection is always a live collection.
A NodeList may look like an array, but in reality, they both are two completely different things. A NodeList object is basically a collection of DOM nodes extracted from the HTML document. An array is a special data-type in JavaScript, that can store a collection of arbitrary elements.
Node and NodeList are general concepts. A Node is, roughly, a distinct object with some internal state. The name Node implies that the object can be or is a member of some collection, often but not always a list. A NodeList is just a linear list of nodes.
The following should return true, if nodes is of type NodeList
NodeList.prototype.isPrototypeOf(nodes)
@DavidSpector, for HTMLCollection you can similarly use :
HTMLCollection.prototype.isPrototypeOf(collection)
I would structure the code differently:
function isNodeList(nodes) { var stringRepr = Object.prototype.toString.call(nodes); return typeof nodes === 'object' && /^\[object (HTMLCollection|NodeList|Object)\]$/.test(stringRepr) && (typeof nodes.length === 'number') && (nodes.length === 0 || (typeof nodes[0] === "object" && nodes[0].nodeType > 0)); }
Notes:
"item"
is not mandatorily in a nodeListhasOwnProperty()
instead of in
nodeType
instead of tagName
, as text nodes or comments do not have a name&&
chain if you see fitIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With