Last active
May 11, 2024 15:22
-
-
Save Tomalak/818a78a226a0738eaade to your computer and use it in GitHub Desktop.
A function to test if a JavaScript object is a DOM NodeList. This is designed to work across all browser implementations. StackOverflow question reference http://stackoverflow.com/a/7238344/18771.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Released under the MIT License in 2014. http://opensource.org/licenses/mit-license */ | |
function isNodeList(nodes) { | |
var stringRepr = Object.prototype.toString.call(nodes); | |
return typeof nodes === 'object' && | |
/^\[object (HTMLCollection|NodeList|Object)\]$/.test(stringRepr) && | |
nodes.hasOwnProperty('length') && | |
(nodes.length === 0 || (typeof nodes[0] === "object" && nodes[0].nodeType > 0)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You'll want to change
nodes.hasOwnProperty('length')
tonodes.length !== undefined
for IE8 support.