2

According to this post, there's an issue with jQuery's filter method in IE8- but according to their API documentation the functionality's been there since 1.4, and nothing is mentioned about browser incompatibility.

Which is it?

I don't have a system with IE8 installed, my customer will use that version and I develop on Foxy and Chromy. The jQuery version on the client's final system is (at least) 1.7.

Community
  • 1
  • 1
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • 1
    There's an issue with the *native* `Array.prototype.filter` method in IE8 and below. The jQuery equivalent will work fine. – James Allardice Jul 12 '13 at 12:15
  • Are you trying to use jQuery's `.filter()` method (which will definitely work in IE8) or the native JS `Array.filter()` method (which won't, and which has nothing to do with jQuery)? – nnnnnn Jul 12 '13 at 12:15
  • No it is not supported in IE8: http://kangax.github.io/es5-compat-table/#Array.prototype.filter – Ron van der Heijden Jul 12 '13 at 12:17
  • "I don't have a system with IE8 installed, my customer **will** use that version". I think I've spotted your problem. I suggest searching for "Windows XP Professional OEM" on eBay, and buying VMWare. – Paul D. Waite Jul 12 '13 at 12:21
  • @PaulD.Waite Or even simpler: switching IE9/10 into IE8 compat mode;) – Christoph Jul 12 '13 at 12:29
  • @Christoph: simpler, yes, but unless that's what the customer's doing, you're not able to run the same software as them, and there's a chance they'll find bugs that you can't reproduce. (I've had this with a couple of IE 8 issues specifically.) – Paul D. Waite Jul 12 '13 at 12:41

2 Answers2

5

The answer in the first article states the native Array.prototype.filter method which is a utility function on native Javascript Arrays (you could write [].filter(...) that way without any framework). This is not supported in IE8.

However, you want to use jQuery's filter() method (to filter dom nodes), which is completely safe to use in IE8;)

Christoph
  • 50,121
  • 21
  • 99
  • 128
  • 1
    I'd be using jQuery's *filter*, definitely. I only reacted to your remark "to filter DOM nodes". Isn't the *filter* of jQuery suitable for **all** kinds of filtration? It seems to me that I can use it on my arrays and stuff seem to work but you got me unsure... – Konrad Viltersten Jul 12 '13 at 13:48
  • 1
    @Konrad sure, you can use it for any kinds of filtering. The most common case is probably filtering dom elements. Sorry for the confusion;) However most likely a plain for loop would be faster that the calling jquery filter. – Christoph Jul 12 '13 at 13:57
1

Native JS array filter is not the same as jQuery filter. If you need the native solution, you can implement a shim/shiv for IE8

IE8 does not support the native filter but DOES support jQuery's until jQuery v2

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

if (!Array.prototype.filter)
{
  Array.prototype.filter = function(fun /*, thisp*/)
  {
    "use strict";

    if (this == null)
      throw new TypeError();

    var t = Object(this);
    var len = t.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var res = [];
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in t)
      {
        var val = t[i]; // in case fun mutates this
        if (fun.call(thisp, val, i, t))
          res.push(val);
      }
    }

    return res;
  };
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236