Oct
31
Posted on 31-10-2007
Filed Under (JavaScript) by Johan Känngård

Internet Explorer does not have an indexOf method in the Array object. Here is how you can add your own:

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

Use it like this:

var a=new Array(\'aaa\',\'bbb\',\'ccc\');
alert(a.indexOf(\'bbb\')); // Shows 1 as it is the index of \'bbb\' in the a Array.
alert(a.indexOf(\'b\')); // Shows -1 since \'b\' is not a member of the a Array.

(0) Comments    Read More