Search code examples
jqueryinternet-explorercompatibility

Function breaking jquery on IE


So i use this function()

months = months.filter(function(e, p) { return months.indexOf(e) == p; });

and a test alert(); works only before it. If i put the alert() somewhere below this function it does not work anymore...
This only happens in IE, in Chrome it is fine. This breaks every jquery below it. You can see the problem live Here

Also, a direct link to the js file is Here

This function is there to filter the repeated months get from all data-mes attributes on li on this page... I have no idea why this is happening, also, i am using this:

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

but, aparently has no effect in this problem....

Here is the full code of this problem:

// gets all `data-mes` into an array
var months = $('#gride li').map(function() { return $(this).data('mes'); }).get();

// remove repeated meses
months = months.filter(function(e, p) { return months.indexOf(e) == p; });

// sorts the months
var order = ['janeiro','fevereiro','março','abril','maio','junho','julho','agosto','setembro','outubro','novembro','dezembro'];
orderedMonths = months.sort(function(a,b){ return order.indexOf(a) - order.indexOf(b); });

// add them, ordered, to the <select> with id #selectMes
$.each(orderedMonths, function(_, v) {
    $('#selectmes').append($('<li class="filter" data-filter="'+ v +'">').val(v).text(v));

});

Solution

  • Use jquery inArray

    months = months.filter(function(e, p) { return $.inArray(e, months) == p });
    

    Array.indexOf not supported below IE9.

    You have an issue with array.filter as well, since filter will not work in IE9 below, instead use $.grep

     months = $.grep(months,function(e, p) { return $.inArray(e, months) == p });
    

    Demo