StealthRT
StealthRT

Reputation: 10542

indexOf in IE8 throwing error but works in all other browsers

Hey all i have the following js code:

for(var i = choicesOrder.indexOf(cat)+1; i<choicesOrder.length; i++)

And its throwing the error: SCRIPT438: Object doesn't support property or method 'indexOf'

How can i go about fixing that since it works in all other browsers?

Upvotes: 0

Views: 639

Answers (2)

Prabin Tp
Prabin Tp

Reputation: 766

You can extend the functionality like this as Fix for .indexOf in IE8 and below

if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function(elt /*, from*/) {
    var len = this.length >>> 0,
        from = Number(arguments[1]) || 0;
    from = (from < 0) ? Math.ceil(from) : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++) {
      if (from in this && this[from] === elt)
        return from;
    }
    return -1;
  };
} 

Upvotes: 0

jishi
jishi

Reputation: 24614

You can find a prototype version that you can implement here: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf

Upvotes: 3

Related Questions