Skip to content Skip to sidebar Skip to footer

JavaScript IndexOf Vs Array.prototype.indexOf IE Compatibility Error

Possible Duplicate: Why doesn't indexOf work on an array IE8? I recently developed a script that uses native Javascript and jQuery. Most of my development has been with IE 9, Ch

Solution 1:

This question has come up many times, another Stackoverflow thread can be found here: How to fix Array indexOf() in JavaScript for Internet Explorer browsers

Several people on different threads have recommended using this code from MDC. This appears to be the code:

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

Just run this code before making any calls indexOf()


Post a Comment for "JavaScript IndexOf Vs Array.prototype.indexOf IE Compatibility Error"