1
var result = Object.prototype.toString.call(nodes);
return (
    typeof nodes === 'object'
    &&
    /^\[object (HTMLCollection|NodeList|Object)\]$/.test(result)
    &&
    nodes.hasOwnProperty('length')
    &&
    (nodes.length == 0 || (typeof nodes[0] === "object" && nodes[0].nodeType > 0))
)

This works properly in Chrome, but throws error in IE8 (probably 7 too). Error is happening at return line.

This I stole from somewhere on here (I'll keep trying to find the link). It's supposed to determine if a given object (nodes) is a node list. Lots of results for this error, but none I could find for this case. Anything jump out at anyone?

Thanks!

Randy Hall
  • 7,716
  • 16
  • 73
  • 151
  • Here's the original article I got the test function from: http://stackoverflow.com/questions/7238177/detect-htmlcollection-nodelist-in-javascript – Randy Hall Nov 19 '12 at 19:22
  • Which object doesn't support which property or method? – Musa Nov 19 '12 at 19:22
  • 3
    This link may perhaps include a fix for your issue. While not related to checking nodelists, your inclusion of the use of hasOwnProperty triggered something in my memory. In IE8, it only works for non Host objects, but there is a work around in the comments: http://stackoverflow.com/questions/8157700/object-has-no-hasownproperty-method-i-e-its-undefined-ie8 – Rick Petersen Nov 19 '12 at 19:24
  • @Musa: IE doesn't actually tell you. It just says that. – gen_Eric Nov 19 '12 at 19:25
  • @RocketHazmat: If one was to use the F12 developer tools and debugged the code, you would know exactly what was causing the error. – Cᴏʀʏ Nov 19 '12 at 19:26
  • @Cory: Using IE's "developer tools"? I've never had luck using those to debug anything, ever. – gen_Eric Nov 19 '12 at 19:27

1 Answers1

2

Thanks to @STLRick:

Object.prototype.hasOwnProperty.call(nodes, 'length')

seems to have fixed it! At least, it's not throwing an error anymore. Not 100% sure if it's affecting the node check, but it is working in the few test cases I am running!

Randy Hall
  • 7,716
  • 16
  • 73
  • 151