Konrad Viltersten
Konrad Viltersten

Reputation: 39180

Can jQuery's filter method be used in IE8?

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.

Upvotes: 2

Views: 2258

Answers (2)

mplungjan
mplungjan

Reputation: 178215

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;
  };
}

Upvotes: 1

Christoph
Christoph

Reputation: 51201

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;)

Upvotes: 5

Related Questions