So IE8 doesn't support Array.indexOf(), so I copied some code that extends the Array prototype so that it is supported and it works.
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (elt /*, from*/) {
var len = this.length >>> 0;
var 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;
};
}
However, I'm running into an issue when I iterate over an array now. Take this for example:
arrayObject = [];
//do some stuff
for(var i in arrayObject) {
...
}
The problem is that after extending the Array prototype, one of the values for i above is now equal to indexOf, and the value at that index (i.e. arrayObj[i]) is equal to the contents of the function. This happens even when the length of the array is zero!! So even when the array is empty, it's still iterating once through for the indexOf entry.
I realize that I can simply do some type checking in the loop, but I'd rather not do that because I'm retrofitting fallback compatibility onto a large existing project. Is there a way to have the indexOf array functionality in IE8 without running into this issue?