Oct
31
Posted on 2007-10-31
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<this .length;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.

    Read More   
Post a Comment
Name:
Email:
Website:
Comments: