String doesn't work in IE
I'm trying to use contains to see if a phrase appears inside a line. The code below works fine in FF and Chrome, however IE8-10 returns an error.
SCRIPT438: Object does not support property or method 'contains'
var str = "This is a string";
if(str.contains("string")){
alert('Yes'};
}
Not sure why IE is throwing the error, so any help would be much appreciated.
source to share
Feature .contains()
is an ES2015 feature that does not support older versions of Internet Explorer.
the MDN page has a polyfill:
if ( !String.prototype.contains ) {
String.prototype.contains = function() {
return String.prototype.indexOf.apply( this, arguments ) !== -1;
};
}
General guidance for such questions: type MDN something
in the google search box. If you don't find a result, then "something" probably doesn't exist in the JavaScript universe. If so, there is a good chance that you will find the answer you are looking for there.
source to share