1

I am not sure if it does or does not. Some sites show using a contains in javascript but when I try it does not work.

I have

var userAgent = navigator.userAgent.split(';');

if (userAgent.contains("msie7") == true) {...}

I am trying to use the useragent to figure out if the user is running IE 8 in compatibility mode. So I want to check the userAgenet for msie7 & for the trident 4.0

So how could I check this array?

chobo2
  • 83,322
  • 195
  • 530
  • 832

6 Answers6

2

There is no native Array.prototype.contains in ES v3 ( most JS implementations ). There is an Array.prototype.indexOf available in Mozilla/Firefox and other modern JS engines which choose to adopt it.

You can use the code below to implement it on browsers/engines that dont have it: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/indexOf

Then after assigning it you can do Array.prototype.contains = function(s) { return this.indexOf(s) !== -1 }

meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
  • Note that he does a `split`, so `userAgent` is an array, not a string. In either case, `indexOf` would work. – casablanca Aug 18 '10 at 01:51
  • How can I make that clearer? I tried userAgent[] but it did not like that. Should I call it userAgentArray(what is bad practice I think in C# but not sure about javascript) – chobo2 Aug 18 '10 at 18:53
2

You can use indexOf to get the index in the array which contains the element you're looking for. So you could rewrite your if statement like this:

if (userAgent.indexOf("msie7") > -1) {...}

Note that IE (at least IE6) doesn't support indexOf for arrays (it does work on strings though), but you could always write your own version:

if (typeof Array.prototype.indexOf == 'undefined')
  Array.prototype.indexOf = function(e) {
    for (var i = 0; i < this.length; i++)
      if (this[i] == e)
        return i;
    return -1;
  }
casablanca
  • 69,683
  • 7
  • 133
  • 150
  • I probably will do that check to see if it is null. I don't support IE 6. I just come up with a reject sign. – chobo2 Aug 18 '10 at 01:55
  • 1
    Neither IE6, 7, or 8 support the method, it was recently implemented on the IE9 Platform Previews. I would recommend [this](https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/indexOf) standards compliant version. It can take a second `fromIndex` argument, uses the strict comparison operator (`===`), etc... – Christian C. Salvadó Aug 18 '10 at 02:01
1

In the end I found out that jquery has a method called inArary and I used this. So I should not have to worry about compatibility issues.

chobo2
  • 83,322
  • 195
  • 530
  • 832
0

You can always roll out your own contains() which will work on Array objects on all browsers.

Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
       if (this[i] === obj) {
          return true;
        } 
    }
    return false;
}

Caveat: "Extending the javascript Array object is a really bad idea because you introduce new properties (your custom methods) into for-in loops which can break existing scripts."

Better use the functions provided by a mainstream library such as jQuery.

Full discussion on this topic: Javascript - array.contains(obj)

Community
  • 1
  • 1
GeneQ
  • 7,485
  • 6
  • 37
  • 53
  • "Caveat" - a good reason not to use `for-in` anyway, because many libraries will extend prototypes without you being aware of it. – casablanca Aug 18 '10 at 17:06
0

Mootools is one library that can help you write better cross browser javascript. It augments javascript basic types(strings, arrays, numbers, etc) with convenient methods like 'contains'.

Or, you can implement it yourself as seen in the other answers.

letronje
  • 9,002
  • 9
  • 45
  • 53
0

You could skip the array and test the string directly, like this:

if (navigator.userAgent.indexOf('msie') != -1)
    // ...

Here's some some information on indexOf.

Casey Chu
  • 25,069
  • 10
  • 40
  • 59