Why Doesn't Indexof Work on an Array IE8

Why doesn't indexOf work on an array IE8?

Versions of IE before IE9 don't have an .indexOf() function for Array, to define the exact spec version, run this before trying to use it:

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;
};
}

This is the version from MDN, used in Firefox/SpiderMonkey. In other cases such as IE, it'll add .indexOf() in the case it's missing... basically IE8 or below at this point.

indexOf not working in IE 8/7

I tried to cast to string in several wasy (as IE 8/7 does not like indexOf on array objects) but to no avail. Thereafter, I found this solution.

  if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(obj, start) {
for (var i = (start || 0), j = this.length; i < j; i++) {
if (this[i] === obj) { return i; }
}
return -1;
}
}

I test if indexOf is supported - and if not the function is created.

IndexOf not Supported in IE8 Browser

The indexOf() method of Arrays is not implemented in IE < 9. As you're using jQuery you can make use of the $.inArray(), e.g.

var arr = ["foo", "bar", "baz"],
bazIndex = $.inArray("baz", arr), // 2
doesntExistIndex = $.inArray("notThere", arr); // -1

Here's the documentation: http://api.jquery.com/jQuery.inArray/.

indexOf causing error in IE; working in other browsers

You should define the method indexOf when it doesn't exist:

if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(obj, start) {
for (var i = (start || 0), j = this.length; i < j; i++) {
if (this[i] === obj) {
return i;
}
}
return -1;
}
}

You should also check this question which has multiple solutions to this problem.

No support for indexOf in IE 8?

IE versions < 9 don't have indexOf, so you can add your own:

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;
};
}

var subItem = [];
subItem[1]="CSMTestPWXListinerService,CSMTestPWXListinerService_ManualyAdded";
console.log(subItem[1].indexOf(","));
//returns 25

.indexOf function on an array not working in IE7/8 using JavaScript

On IE<9 indexOf() it is not "well" implemented. Try to add this function on your code :

if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length;

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;
};
}

It will "replace" the original function, if not found in the ECMA-262 standard.

How to fix Array indexOf() in JavaScript for Internet Explorer browsers

Do it like this...

if (!Array.prototype.indexOf) {

}

As recommended compatibility by MDC.

In general, browser detection code is a big no-no.



Related Topics



Leave a reply



Submit