admin管理员组文章数量:1302545
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.
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.
Share Improve this question edited Nov 3, 2014 at 17:58 rink.attendant.6 46.3k64 gold badges110 silver badges157 bronze badges asked Nov 3, 2014 at 17:57 user3515428user3515428 592 silver badges4 bronze badges 2-
1
MDN
String.contains
First check documentation, then check Google. Asking someone for help es later down the line. – user1106925 Commented Nov 3, 2014 at 17:59 -
Also your
alert
probably should bealert('Yes');
notalert('Yes'};
with a brace. – phantom Commented Nov 3, 2014 at 18:00
1 Answer
Reset to default 9The .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.
本文标签: javascriptString contains not working in IEStack Overflow
版权声明:本文标题:javascript - String contains not working in IE - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741668432a2391454.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论