2

I found strange for the following code:

var allextRules = Ext.util.CSS.getRules();

Object.keys(allextRules).forEach(function(key) {
    var keyname = key;
    if(keyname.indexOf("js") != -1){
        Ext.util.CSS.removeStyleSheet(keyname);
        console.log(keyname + " Removed");
    }
});

When the above work is tested in other browser (say - Google Chrome), there is no error. However, when tested in IE 9, there is error as follows:

SCRIPT438: Object doesn't support property or method 'keys' 

According to this article (https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys), the Object.keys is supported by IE.

Have I miss out something?

2
  • have you got ie9 in ie9 broswer mode in developer tool bar?
    – thiswayup
    Feb 8, 2012 at 16:03
  • 1
    Also, have you tried defining the function via the code in the "Compatibility" section of the link you gave? Even if IE9 does actually support the keys method, defining it yourself won't hurt. And if it still doesn't work, then you have a better understanding of your problem
    – phatskat
    May 28, 2012 at 1:48

2 Answers 2

10

Try add this code before your

    if (!Object.keys) {
  Object.keys = (function () {
    var hasOwnProperty = Object.prototype.hasOwnProperty,
        hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
        dontEnums = [
          'toString',
          'toLocaleString',
          'valueOf',
          'hasOwnProperty',
          'isPrototypeOf',
          'propertyIsEnumerable',
          'constructor'
        ],
        dontEnumsLength = dontEnums.length;

    return function (obj) {
      if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object');

      var result = [];

      for (var prop in obj) {
        if (hasOwnProperty.call(obj, prop)) result.push(prop);
      }

      if (hasDontEnumBug) {
        for (var i=0; i < dontEnumsLength; i++) {
          if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]);
        }
      }
      return result;
    }
  })()
};

here is more details about this issue - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys

2
  • I just added the above code to the top of my script that needed the hasOwnProperty method and IE 8 instantly started working again. Thank you!!!
    – dmulvi
    Dec 10, 2013 at 19:45
  • I know why this doesn't work in IE for some people: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Array.prototype.keys() doesn't work for IE (up to 11) so this is why it might not be working for some. Jan 6, 2016 at 1:14
3

in method 'forEach' (error script438: object doesn't support this property or method 'foreEach', in IE 9), the soluction was:

if (!Array.prototype.forEach) {
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

Font: http://www.tutorialspoint.com/javascript/array_foreach.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.