简体   繁体   中英

jQuery - Object doesn't support this property or method in IE8

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.

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 .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM