trim() function doesn't work in IE8?

前端 未结 4 1111
栀梦
栀梦 2020-12-25 11:35

Whenever I use the trim() function on a string, it works fine with Chrome and Firefox but I get an error in IE8 saying :

Object doesn\'t

4条回答
  •  不思量自难忘°
    2020-12-25 12:30

    Internet Explorer only started support for trim() from version 9.

    For reference, the MDN Polyfill for String.prototype.trim() is:

    if (!String.prototype.trim) {
      (function() {
        // Make sure we trim BOM and NBSP
        var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
        String.prototype.trim = function() {
          return this.replace(rtrim, '');
        };
      })();
    }
    

    and the support for it is:

    +--------+---------+----+-------+--------+
    | Chrome | Firefox | IE | Opera | Safari |
    +--------+---------+----+-------+--------+
    | All    | 3.5     |  9 | 10.5  |      5 |
    +--------+---------+----+-------+--------+
    

提交回复
热议问题