Erica Stockwell-Alpert
Erica Stockwell-Alpert

Reputation: 4853

indexOf causing error in IE; working in other browsers

EDIT: I'm super confused. I have a local copy of the webpage, in which I added the 'indexOf' function to the javascript file. This page is working. However, the page on the live site, which I have pushed my changes to, is still getting the indexOf error. Furthermore, another page on the site that uses the exact same script is not getting any errors. There shouldn't be a problem with giftFeatures[giftB], because according to the debugger it is an array.

This is a URL for the problem page

This is a page that uses the exact same script with no errors

What is the difference between those two pages, why does the second work but the first doesn't? This is driving me crazy.


I have a script attached to a page that is working in all browsers except IE (using IE11). Here's the method that's not working:

  buildComparisonData: function() {
  this.comparisonData = [];
  var giftA = this.selectedGifts[0];
  var giftB = this.selectedGifts[1];
  if(this.selectedGifts.length == 3)
   var giftC = this.selectedGifts[2];

  for(var i = 0; i < this.config.giftFeatureLabels.length; i++) {
    var label = this.config.giftFeatureLabels[i].feature;
    var checkimg = '<img src="https://www.giftcalcs.com/sites/all/modules/custom/pgc_giftcompare/check-mark.png">';
    var giftBChecked = this.config.giftFeatures[giftB].indexOf(i) < 0 ? '' : checkimg;
    var giftAChecked = this.config.giftFeatures[giftA].indexOf(i) < 0 ? '' : checkimg;
    if(this.selectedGifts.length == 3)
     var giftCChecked = this.config.giftFeatures[giftC].indexOf(i) < 0 ? '&nbsp;' : checkimg;

    var row = {
      label: label,
      giftA: giftAChecked,
      giftB: giftBChecked
    };
    if(this.selectedGifts.length == 3)
      row.giftC = giftCChecked;

    this.comparisonData.push(row);

    var comparisonLabels = {};
    comparisonLabels.giftA = this.getGiftLabel(this.selectedGifts[0]);
    comparisonLabels.giftB = this.getGiftLabel(this.selectedGifts[1]);
    if(this.selectedGifts.length == 3)
      comparisonLabels.giftC = this.getGiftLabel(this.selectedGifts[2]);

    this.comparisonLabels = comparisonLabels;
  }

the line where it breaks is

var giftBChecked = this.config.giftFeatures[giftB].indexOf(i) < 0 ? '' : checkimg;

It says "Object doesn't support property or method 'indexOf'

The script has no errors in Chrome and Firefox. I'm able to get indexOf on giftFeatures[giftB], but not in IE.

Upvotes: 0

Views: 2163

Answers (1)

Catalin MUNTEANU
Catalin MUNTEANU

Reputation: 5634

You should define the method indexOf when it doesn't exist:

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(obj, start) {
        for (var i = (start || 0), j = this.length; i < j; i++) {
            if (this[i] === obj) {
                return i;
            }
        }
        return -1;
    }
}

You should also check this question which has multiple solutions to this problem.

Upvotes: 2

Related Questions