6

This function executes during the forms onSubmit, and works fine in Firefox and Chrome, but not in IE. I suspect it's indexOf, but I cannot seem to find a way to get it to work.

function checkSuburbMatch(e) {

var theSuburb = document.getElementById('suburb').value;
var thePostcode = document.getElementById('postcode').value;

var arrayNeedle = theSuburb + " (" + thePostcode + ")";

if(suburbs.indexOf(arrayNeedle) != -1) {
    alert("Suburb and Postcode match!");
    return false;
} else {
    alert("Suburb and Postcode do not match!");
    return false;
}

}
David
  • 61
  • 1
  • 1
  • 2
  • What does variable "suburbs" contain? – Tim Sep 13 '10 at 02:30
  • It's an array of strings going along the lines of "suburbName (postCode)" – David Sep 13 '10 at 03:42
  • possible duplicate of [Why doesn't indexOf work on an array IE8?](http://stackoverflow.com/questions/3629183/why-doesnt-indexof-work-on-an-array-ie8). See also: [How to fix Array indexOf() in JavaScript for IE browsers](http://stackoverflow.com/questions/1744310/), [Array indexOf implementation for Internet Explorer](http://stackoverflow.com/questions/2868696) – Christian C. Salvadó Sep 13 '10 at 04:20
  • For a thorough explanation of the issue as well as a work around not only for indexOf but the other missing array functions in IE check out the StackOverflow question http://stackoverflow.com/questions/2790001/fixing-javascript-array-functions-in-internet-explorer-indexof-foreach-etc – Luis Perez Jan 11 '12 at 02:43
  • FWIW I have a Dev environment where it works with IE11, but when I promoted to my Test environment it stopped working?!?! So it's like IE has a half-baked implementation out there somewhere – gordon Nov 06 '15 at 20:59

4 Answers4

18

IE simply doesn't have this method on Array, you can add it yourself though, from MDC:

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 adds .indexOf() if it's missing (at this point that means you're in IE<9) then you can use it. As for why even IE8 doesn't have this already? I can't help you there...

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
10

If you are already using jQuery in your project you can use $.inArray()

http://api.jquery.com/jQuery.inArray/

jhanifen
  • 4,441
  • 7
  • 43
  • 67
0

indexOf() on MSIE 11 and others it doesn't like non-string variables. On suburbs add .toString() and it should fix it.

SpookySr
  • 11
  • 1
-1

this function is bad when using associative arrays.

if you put that function in your code and do this

var a = new Array();

a["one"] = "1";

for(var i in a){

   alert(i)

}

You get 0, indexOf which means you inserted indexOf as a key to every array you create

but the array should only have one key and that is "one"

use jQuery!

-Mekias

Echilon
  • 10,064
  • 33
  • 131
  • 217
Mekias
  • 1