Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why javascript contains property is not working in chrome browser?

Tags:

javascript

Why javascript contains property is not working in chrome browser? I have tried that Contains Property in javascript.It is working fine in Mozila Firefox Browser. But It is not working in Chrome browser.How to fix this?

Link: http://www.codingslover.com/2014/11/why-javascript-contains-property-is-not.html

var ClearFilterValue='family Schools'; if(ClearFilterValue.contains("family")== true) {       alert('Success'); } 
like image 886
Elangovan Avatar asked Oct 25 '13 12:10

Elangovan


2 Answers

indexof returns the position of the string. If not found, it will return -1:

var ClearFilterValue = 'family Schools'; alert(ClearFilterValue.indexOf("family") != -1); 
like image 132
Prateek Avatar answered Sep 26 '22 16:09

Prateek


contains is not supported in Chrome, but you could use a polyfill:

if (!String.prototype.contains) {     String.prototype.contains = function(s) {         return this.indexOf(s) > -1     } } 'potato'.contains('tat') // true 'potato'.contains('tot') // false 
like image 20
tckmn Avatar answered Sep 23 '22 16:09

tckmn