Reputation: 59
I am trying to use contains to find out if a phrase appears within a string The code below works fine in FF and Chrome, however IE8-10 return an error.
SCRIPT438: Object doesn't support property or method 'contains'
var str = "This is a string";
if(str.contains("string")){
alert('Yes'};
}
Not sure why IE is throwing a error so any help would be much appreciated.
Upvotes: 3
Views: 7429
Reputation: 413976
The .contains()
function is an ES2015 feature that older Internet Explorer versions don't support.
The MDN page has a polyfill:
if ( !String.prototype.contains ) {
String.prototype.contains = function() {
return String.prototype.indexOf.apply( this, arguments ) !== -1;
};
}
A general guide for questions like this: type MDN something
into the Google search box. If you don't find a result, then "something" probably doesn't exist in the JavaScript universe. If you do, then there's a pretty good chance that you'll find the answer you seek there.
Upvotes: 9