最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - IE8 shows error: "Object doesn't support this property or method" on the line that uses f

programmeradmin3浏览0评论

I am using this script to limit the characters on lines 1-3 on my textarea. It works in Firefox and Chrome. But in IE8, it shows an error: "Object doesn't support this property or method" on the line that uses filter() method.

Here's the code:

var result = jQuery('#result');
var my_textarea = jQuery('#mytext');
my_textarea.on('keyup', function(event){
    var el = jQuery(this);
    var lines = el.val().split('\n').length;
    var chars = el.val().split('').filter(function(v){
        return v != '\n';
    }).length;    
    result.html('You have ' + lines + ' lines and ' + chars + ' chars');    

    if ((lines === 1 && chars > 20) || (lines === 2 && chars > 40) || (lines === 3 && chars > 60)) {
          my_textarea.val( my_textarea.val() + "\n");
    }
});

How do I resolve this?

I am using this script to limit the characters on lines 1-3 on my textarea. It works in Firefox and Chrome. But in IE8, it shows an error: "Object doesn't support this property or method" on the line that uses filter() method.

Here's the code:

var result = jQuery('#result');
var my_textarea = jQuery('#mytext');
my_textarea.on('keyup', function(event){
    var el = jQuery(this);
    var lines = el.val().split('\n').length;
    var chars = el.val().split('').filter(function(v){
        return v != '\n';
    }).length;    
    result.html('You have ' + lines + ' lines and ' + chars + ' chars');    

    if ((lines === 1 && chars > 20) || (lines === 2 && chars > 40) || (lines === 3 && chars > 60)) {
          my_textarea.val( my_textarea.val() + "\n");
    }
});

How do I resolve this?

Share Improve this question asked Feb 1, 2012 at 2:30 catandmousecatandmouse 11.8k24 gold badges95 silver badges157 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 10

IE8 doesn't support the Array.filter method.
MDN has a replacement.

filter (and several other methods on Array) are relatively new and aren't implemented in older browsers - not just IE8, but older Firefox too.

You may be interested in array_filter from PHPJS, as this produces the same effect.

Then again, looking at your code, wouldn't this be simpler?

var chars = el.val().replace(/\n/g,'').length;

Array.filter js polyfill:

[].filter||(Array.prototype.filter=function(g,f,j,i,h){
  j=this;i=[];
  for(h in j){~~h+""==h&&h>=0&&g.call(f,j[h],+h,j)&&i.push(j[h])}
  return i
});

Jakob E

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论