4

I have a productlist and i want to scarch the productlist by input parameter. so I was using contains for scarching the input string in the productlist. its working fine with web page. but when i open the same page in mobile web page its not working. and giving an error that 'contains' in not define.

if(productlist[i].name.toLowerCase().contains(input_val.toLowerCase()))

    --my business logic--

after that i have tried with indexOf then its working fine in both cases.

if(productlist[i].name.toLowerCase().indexOf(input_val.toLowerCase()) !== -1)
    --my business logic --

what is the problem for .contains??

Kousik
  • 21,485
  • 7
  • 36
  • 59
  • 1
    Maybe your andriod browser doesn't support the function? Just use `indexOf`. And what are you using `eval` for? – Shawn31313 Jul 20 '13 at 18:25
  • 1
    Shawn is correct, according to Mozilla, Android does not support contains. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/contains – JConstantine Jul 20 '13 at 18:27
  • 1
    .contains is a JavaScript Harmony feature (ES6/ES.next). You can create your own `contains` function however. `String.prototype.contains = function () { ... };` – Shawn31313 Jul 20 '13 at 18:28
  • i was using eval coz productlist was comming from server with different format eval change the format to string . nway thanks @JLevett – Kousik Jul 20 '13 at 18:32
  • @KousikChowdhury There is never an excuse to use eval. You could use `String(productlist)` or `productlist.toString()` or if it was an object `JSON.stringify(productlist)` – Shawn31313 Jul 20 '13 at 18:36

1 Answers1

2

Use this Polyfill, ( Reference : MDN)

if(!('contains' in String.prototype)) {
  String.prototype.contains = function(str, startIndex) { 
      return -1 !== String.prototype.indexOf.call(this, str, startIndex); 
  };
}

See the Compatibility table in Can I use...

Update : Also you can check this answer.

Community
  • 1
  • 1
The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • @Shawn31313, Yes, definitely it's but the question is `'.contains' is not working in android phones but its working fine in web page` so answer the relevant one. – The Alpha Jul 20 '13 at 18:39