0

I am having an javascript issue where internet explorer 7 and 8 are giving an error in the console.

I think the the keys method maybe is not supported? This is the piece of code causing the problem:

if (count == Object.keys(aResults).length) {
    if (typeof Object.keys === 'function') {
        globalPriceGroupKey = Object.keys(globalPriceGroup[colorID]);
    } else {
        for (var key in globalPriceGroup[colorID]) {
            globalPriceGroupKey.push(key);
        }
    }

    //globalPriceGroup[colorID].sort( function numOrdA(a, b){ return (a-b); } );
    globalPriceGroupKey.sort(function(a, b) {
        return globalPriceGroup[colorID][a] - globalPriceGroup[colorID][b];
    });
}

Is there anything I can do to this code to make it compatible with ie7/8?

Mörre
  • 5,699
  • 6
  • 38
  • 63
Sackling
  • 1,780
  • 5
  • 37
  • 71
  • 2
    Object.keys isn't supported in older IE, should be obvious from the error message ? – adeneo Dec 08 '13 at 19:41
  • 2
    Look it up on [**MDN**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys), and at the bottom you'll see browser support. – adeneo Dec 08 '13 at 19:42
  • 4
    Strange code - in the very first line you use `Object.keys()` - to be followed immediately by an `if`statement that checks if that function exists! Did you get that sequence wrong? After I cleaned up your indents it became (more) obvious ;-) – Mörre Dec 08 '13 at 20:01

1 Answers1

5

Yes. This isn't difficult at all. A very simple shim would look like this:

if (!Object.keys) {
    Object.keys = function(obj) {
        var keys = [],
            key;

        for (key in obj) {
            if (obj.hasOwnProperty(key)) {
                keys.push(key);
            }
        }

        return keys;
    };
}

A much better version is listed on MDN, which often provides useful shims.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318