Why is indexOf not working in Internet Explorer?
Asked Answered
S

4

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

}
Swirly answered 13/9, 2010 at 2:26 Comment(5)
What does variable "suburbs" contain?Mendelevium
It's an array of strings going along the lines of "suburbName (postCode)"Swirly
possible duplicate of Why doesn't indexOf work on an array IE8?. See also: How to fix Array indexOf() in JavaScript for IE browsers, Array indexOf implementation for Internet ExplorerMako
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 #2790501Egon
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 somewhereCorpuscle
G
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...

Groan answered 13/9, 2010 at 2:29 Comment(2)
This didn't seem to work, and made it stop working in Firefox too. Does this need to be placed after the function is called or at the start of the script?Swirly
@David: It needs to be declared before it is called.Presbyter
E
10

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

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

Enrica answered 13/9, 2010 at 2:33 Comment(0)
F
0

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

Fronia answered 23/7, 2014 at 16:36 Comment(0)
M
-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

Mammillate answered 18/2, 2011 at 20:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.