2

I have implemented sticky notes functionality in one of my projects and I have found a problem related to browser compatibility. My code is not working in IE8, but it works fine in other browsers.

The error I get is:

Message: Object doesn't support this property or method
Line: 45
Char: 3
Code: 0
URI: http://dev.mesocial.co/orange_theme/js/sticky_note_func.js

These are the lines between no 40 to 50:

var moved = function(note) {

    // added by rupesh 
    // alert(JSON.stringify(lastCreatedNoteId)); 
    var passId = note.id
    if(lastCreatedNoteIdForJs.indexOf(note.id) != -1)
         passId = lastCreatedNoteId[note.id];
    // till here ///////
    $.post(SITE_URL+'/dashboard/create-sticky-note/act/moved/sticky_note_id/'+passId+'/pos_x/'+note.pos_x+'/pos_y/'+note.pos_y,
    function(data) {
        //alert(data);
    });

Any help on what the problem might be would be much appreciated.

Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
Ekta Patel
  • 100
  • 2
  • 7

3 Answers3

7

IE8 does not support .indexOf(). In Internet Explorer it was first introduced with IE9.

Since you are using jQuery, you can use the $.inArray() method instead.

Something like this, in your case:

if($.inArray(note.id, lastCreatedNoteIdForJs) != -1)

Support for older browsers without jQuery:

If you want to use .indexOf() in legacy browsers as well, you use your own array-prototype when needed. The prototype would look something like this:

if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
    "use strict";
    if (this == null) {
      throw new TypeError();
    }
    var t = Object(this);
    var len = t.length >>> 0;

    if (len === 0) {
      return -1;
    }
    var n = 0;
    if (arguments.length > 1) {
      n = Number(arguments[1]);
      if (n != n) { // shortcut for verifying if it's NaN
        n = 0;
      } else if (n != 0 && n != Infinity && n != -Infinity) {
        n = (n > 0 || -1) * Math.floor(Math.abs(n));
      }
    }
    if (n >= len) {
      return -1;
    }
    var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
    for (; k < len; k++) {
      if (k in t && t[k] === searchElement) {
        return k;
      }
    }
    return -1;
  }
}

Above example is taken from the MDN article on Array.prototype.indexOf.

Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
  • @EktaPatel Glad it worked! You should consider accepting the answer that was most useful to you by clicking the checkmark to left of that question. You can read more about why you should accept an answer [here](http://stackoverflow.com/faq#howtoask). – Christofer Eliasson Mar 04 '13 at 10:09
1

Check this link it may help you... IndexOf wil work only in certain browsers ..

How to fix Array indexOf() in JavaScript for Internet Explorer browsers

Community
  • 1
  • 1
Kiren S
  • 3,037
  • 7
  • 41
  • 69
0

IE < 9 doesn't support the .indexOf method for arrays.

jQuery has a method which allows you to check if the element is in the array.

if($.inArray(note.id, lastCreatedNoteIdForJs) !== -1) {
    passId = lastCreatedNoteId[note.id];   
}

You could also add the method to the Array object: How to fix Array indexOf() in JavaScript for Internet Explorer browsers

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;
    }
}
Community
  • 1
  • 1
Emil A.
  • 3,387
  • 4
  • 29
  • 46