12

I've been trying to debug some js in Internet Explorer, and I can't figure this one out. Here's the line that is causing the error:

var numberOfColumns = Object.keys(value).length;

And the error is...

Message: Object doesn't support this property or method
Line: 640
Char: 5
Code: 0
URI: xxx

At first I thought it had something to do with the Object.keys(value).length; property, but strangely (for me anyways), the error is at char 5, which is the beginning of the variable name.

Anyways, I have no idea what's going on or how to fix it. Also, if I replace:

var numberOfColumns = Object.keys(value).length;

With ...

var numberOfColumns = 9; // troubleshooting

The error persists. Please help.

Update

jsFiddle added

http://jsfiddle.net/4Rab7/

veryConfused
  • 137
  • 1
  • 1
  • 6
  • did you try to run it with firebug, or any other debug tool? – AMember Dec 05 '12 at 13:01
  • can you show some more code, maybe a jsfiddle with the issue – AMember Dec 05 '12 at 13:02
  • If you replace the supposedly faulty code with non-faulty code and it still fails, odds are the problem is something else. – Fabrício Matté Dec 05 '12 at 13:05
  • My company blocks certain sites, and it just so happens the FF addons page, as well as a tool I found in this post: http://stackoverflow.com/questions/361635/debugging-javascript-in-ie7, are blocked. But yeah, I'll have a jsfiddle ready in just a second – veryConfused Dec 05 '12 at 13:05
  • I updated the question with a simple jsFiddle implementation. In IE 8, there is no alert, but in Firefox, it works. – veryConfused Dec 05 '12 at 13:46
  • Use the developer tools (F12) to determine whether a feature is supported in your browser or not. For instance, typing `Object.keys` into the console of both IE10 and Chrome reveal `function keys() { [native code] }`, showing they support this method. – Sampson Dec 05 '12 at 16:12

3 Answers3

23

The keys property is supported in IE >= 9. You are probably testing it in an earlier version. A simple workaround is:

var length = 0;
for(var prop in data){
    if(data.hasOwnProperty(prop))
        length++;
}

Here is a demonstration: http://jsfiddle.net/vKr8a/

See this compatibility table for more info:

http://kangax.github.com/es5-compat-table/

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
13

Alternatively, you could use a recommended polyfill for browsers that don't natively support Object.keys

Object.keys=Object.keys||function(o,k,r){r=[];for(k in o)r.hasOwnProperty.call(o,k)&&r.push(k);return r}

A break down of what this script does:

Object.keys = Object.keys || function(o,k,r) { 
// If the script doesn't detect native Object.keys 
// support, it will put a function in its place (polyfill)

    r=[];
    // Initiate the return value, empty array

    for(k in o) r.hasOwnProperty.call(o,k) 
    // loop through all items in the object and verify each
    // key is a property of the object (`for in` will return non 
    // properties)

    && r.push(k);
    // if it is a property, save to return array

    return r
}
digout
  • 4,041
  • 1
  • 31
  • 38
4

Object.keys has been introduced in ECMAScript 5th Edition. So if you IE-version is lower than 9, it will not be supported.

nozzleman
  • 9,529
  • 4
  • 37
  • 58