soledad penadés
repeat 4[fd 100 rt 90]

Array.indexOf in Internet Explorer

According to this document at Mozilla Developer Center, Javascript 1.5 has been implemented in a browser since at least the first releases of Mozilla as open source browser, which means, in other words, since around 1998. Let's assume it was 2002 which is marked as the release of the 1.0 version.

And I was doing some interface programming lately and I needed to check if an element was in an array. Went to Gecko's documentation (that's the one I normally use, since it's less overbloated with crappy ads) and checked that Array objects had an indexOf function. Cool! I used that function on my code. Then once I finished with all the development, went to The Horror (i.e. Internet Explorer) and tested it.

Surprise! It was broken! What had I done? (You know the debugger for Internet Explorer is not specially helpful)

I suspected of the indexOf function, and recalled vague memories about doing a custom search function in the past for looking into arrays and not having to write a for(i=0; i<ar.length;i++) thing each time…

Mmmm… did a alert('Array.indexO') in ie and what did I get?: undefined

So they have been spent five years for releasing ie7 and still they didn't implement Array.indexOf!

No worries, though. Javascript is flexible! Look, IE, I don't care if you choke on the mere seeing of indexOf, you're going to run it whether you like it or not!

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

and voila! my script wasn't broken anymore!

// 42 responses to Array.indexOf in Internet Explorer

herotyc
herotyc
20070517

I was just curious about the way mootools handled this(to be compatible with IE):

http://svn.mootools.net/tags/1-10/Native/Array.js

It's just like you did. Btw, have you tried mootools? "will make you happier" :D

sole
sole
20070517

Funny! I was wondering if there could be a better way of doing it but I was in a rush… I haven't played yet with mootools, since I started with jquery and didn't want to go back and rewrite that project with mootools :)

But I have something in mind and will probably have a look at moo for that project just before starting to code in jquery like crazy!

blackpawn
blackpawn
20070517

whoa that's crazy.. how could they have missed that?? hmm maybe it's missing to incentivize you to use VBScript instead! ;)

sole
sole
20070518

yay no! no vbscript!

Which reminds me something that happened to me past year: i wanted to look for some information about rugsacks on an sports retailer site and began to click everywhere in the menu, with no luck. Nothing was happening, and I couldn't see any Javascript error in the debug console. I thought it might be some IE only stuff going on there, but before abandoning completely, I took a look at the source and guess what!

All the navigation was built with VBScript! I couldn't believe it, and the worst of all is that the page was quite recently built. Absolutely fascinating.

Kr0n
Kr0n
20070518

Nice!

To improve it even more:

When prototyping, the new method should extend all the functionality of the existing method, in order to use it completely transparent. In this case, the "start" parameter is missing. Obviously, no such a big deal but being perfectionists… ;)

jo
jo
20070725

Mozilla provide the full javascript implementation for compatibility:
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:indexOf

scott
scott
20070914

That you for publishing this find, you saved me!

Nick Mudge
Nick Mudge
20071218

Good stuff. Thanks.

sole
sole
20071219

Thanks Nick, it's a big compliment coming from you - I have been reading your blog and it's got loads of interesting notes and thoughts.

Gargaj
Gargaj
20080117

(What irony that I search for the same thing and end up here, eheh)

Apparently, it's not part of the DOM anyway: http://www.w3schools.com/jsref/jsref_obj_array.asp :( (had the same problem with Opera)

sole
sole
20080117

I own your internets!!!! HAHA!

No, really. I guess it's something more of a de-facto standard, or an ECMA Script standard. Or simply a common sense standard. Whatever! :-)

Stan
Stan
20080205

Nice fix, this had me stumped for a while with some validation which was working fine in Firefox but borked in IE.

Cheers!

Kemot
Kemot
20080211

Great! It helped me a lot. Thanks.

sole
sole
20080211

Great to read that :)

bob
bob
20080218

Thanks for this - ! I was scratching my head over why my indexOf wasn't working also. Good to see someone is one step ahead of Bill Gates.

Shawn Welch
Shawn Welch
20080307

Hey, you are cute. Thanks for the tip. IE 7 blows!!!

Raphael
Raphael
20080307

Thanks.
That's exactly what I was looking for.

sole
sole
20080308

No problem. I wonder if this will be fixed in IE8 … might check it…

Felix Geisendörfer
Felix Geisendörfer
20080309

Google rank 1 for 'ie indexof array', not bad ; ).

I just hoped for a second this was working in IE, but I guess we'll have to wait for 2012 the year when IE ends … : ).

sole
sole
20080309

Oh, mighty Felix "thinking php" Geisendörfer here :)

Yes we come to expect lots of things even on newer versions such as IE7 but it's still as buggy as usual. Now I always assume it won't work, and maybe it will surprise me later :D

webdesign
webdesign
20080321

Wow thanx i was really stuck with this piece of javascript !! have spent several hours check IE and FF for what is wrong :) Thx again

Manoj Mathai
Manoj Mathai
20080402

Thanks man, that helped

M1ke
M1ke
20080411

Thanks a lot for this - been banging my head against a wall trying to translate a nice working Firefox site into something other than a static lump in Internet Explorer. Eventually realised that it was hating indexOf and Googled - came straight here!

sole
sole
20080411

Super :)

Audren Cezar
Audren Cezar
20080419

Thx, very good trick! ;)

yves geunes
yves geunes
20080422

I ran into the same problem. This solved it:

myString=Array.toString();
theIndex=myString.indexOf("something");

James Beckett
James Beckett
20080429

Mmm. When you look at something like http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:indexOf you have to keep an eye out for that "ECMAScript Edition: none" tag! I was caught out myself by indexOf this week.

This and others are in the proposals for ECMAScript 4 - http://wiki.ecmascript.org/doku.php?id=es3.1:targeted_additions_to_array_string_object_date&s=indexof - but don't expect to see anything interesting before (or soon after!) October 2008.

@yves geunes: myString.indexOf("something"); isn't going to achieve the same thing, unless you know that "something" contains no commas etc which could match element boundaries. It's conceptually not the same search.

sole
sole
20080429

@James - you're right on the ECMAScript edition "little detail", and on clarifying yves solution too. I was going to comment on that but I haven't had time these past days.

In any case… six years and there's still no support for it in IE? :-) They followed the 'defacto' standard with the tabs in IE7, but they gave no love to the scripting engine it seems :-)

Frank (Onstuimig.nl)
Frank (Onstuimig.nl)
20080509

@yves geunes
Array.toString()

The toString() is ok but you could also use the join() method like this:
arrayObject.join().indexOf("look for")

Just one warning, it is best to use this only for "single caracter" array elements like in:
var arrayObject = new Array("a","b","c");

Adam
Adam
20080523

Thank you so much. This article saved my project! :)

Stupid IE…

thirdender
thirdender
20080614

Found your site through Google search… Was playing with the awesome PersistJS library (http://pablotron.org/?cid=1557) and it wigged out when the 'remove' function tried indexOf on an Array. I guess I'm supposed to be using Prototype or something to smooth the IE glitches… :-P

Julie
Julie
20080616

This saved me too!

Dale
Dale
20080622

Holy cannoli, Soledad, you saved my butt. My navigation scripts worked great on FF, and my heart sank to see them dead in the water on IE. After a fruitless search for decent IE debug tools, I finally located the offending line…which included indexOf. Googled "javascript indexOf IE", your site came up, and I pasted in that lovely little code snippet and I'm back in business.

Blessings on your family. Dalegeist.com will, thanks to you, be up and running in the twinkle of an eye.

Dale

Venu
Venu
20080623

Thanks a lot to publish this. It's working great in IE and as well as in Mozilla. :)

Vijay Mohan
Vijay Mohan
20080624

hi
its realy good workin well with little modification

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

Marius
Marius
20080625

Array.indexOf(value)

Simple jQuery solution:
jQuery.inArray(value, Array)

Faseel
Faseel
20080705

extending the Array.prototype may work if you are only using Array.indexOf()
but it breaks the Array object.

the following will give unexpected results on IE if the Array.prototype is extended.
for(var i in data) will output the Array.indexOf function as well.
log: function (data) {
for (var i in data){
console.log(data[i].string);
}
}
},
if the Array is extended, then a typeof check is required before performing any operation with arrays.

Switch
Switch
20080808

Thanks man, this was VERY helpful! Stupid IE!

sole
sole
20080808

hihihi :)

m
m
20080821

Thanks great fix!

Just one more reason to stop using IE!

Greg
Greg
20080903

Nice, this helped me a lot.

IE was driving me up a wall.

Feel free to leave a reply

Comments are moderated: Rude and offtopic ones are out!