why javascript contains property is not working in chrome browser?

前端 未结 6 1312
孤城傲影
孤城傲影 2021-02-01 15:39

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

6条回答
  •  死守一世寂寞
    2021-02-01 16:21

    Actually String.contains is not supported in Chrome as per the MDN

    Here's the way to resolve the issue:

    Polyfill

    You can easily polyfill this method :

    if (!('contains' in String.prototype)) String.prototype.contains = function (str, startIndex) {
        return -1 !== String.prototype.indexOf.call(this, str, startIndex);
    };
    
    var ClearFilterValue = 'family Schools';
    if (ClearFilterValue.contains("family") == true) {
        alert('Success');
    }
    

    Demo: Fiddle

提交回复
热议问题