admin管理员组

文章数量:1293661

IE8 is giving me the following error:

Object doesn't support this property or method on custom.js, line 82 character 7

Here is the code with line numbers:

78 function transpose(chord, increment){
79   var scale = ["C", "C#", "D", "Es", "E", "F", "F#", "G", "As", "A", "B", "H"];
80   return chord.replace(/[CDEFGABH]#?s?/g,
81   function(match){
82      var i = (scale.indexOf(match) + increment) % scale.length;
83      return scale[ i < 0 ? i + scale.length : i ];
84   });
85 }

What should I change so that the code works in IE8? It works properly in Firefox/Chrome and also in IE9.

IE8 is giving me the following error:

Object doesn't support this property or method on custom.js, line 82 character 7

Here is the code with line numbers:

78 function transpose(chord, increment){
79   var scale = ["C", "C#", "D", "Es", "E", "F", "F#", "G", "As", "A", "B", "H"];
80   return chord.replace(/[CDEFGABH]#?s?/g,
81   function(match){
82      var i = (scale.indexOf(match) + increment) % scale.length;
83      return scale[ i < 0 ? i + scale.length : i ];
84   });
85 }

What should I change so that the code works in IE8? It works properly in Firefox/Chrome and also in IE9.

Share Improve this question asked Nov 7, 2011 at 9:40 FrantisekFrantisek 7,70316 gold badges64 silver badges102 bronze badges 5
  • Try a indexOf polyfill – Yoshi Commented Nov 7, 2011 at 9:43
  • What arguments are you passing to the function when calling it? – Darin Dimitrov Commented Nov 7, 2011 at 9:46
  • @Yoshi: that solution works. any idea whether I should put the code inside document ready, or outside? I have it outside now, not sure whether it's ok. DarinDimitrov: 'chord' is a string, 'increment' is either 1 or -1 – Frantisek Commented Nov 7, 2011 at 9:48
  • Should be ok to have it outside, as there is no need to have the dom or window being ready to apply this fix. – Yoshi Commented Nov 7, 2011 at 9:49
  • @Yoshi please submit that as an answer, so that I can accept it? – Frantisek Commented Nov 7, 2011 at 9:53
Add a ment  | 

1 Answer 1

Reset to default 10

You can try a indexOf polyfill, this is needed due to indexOf being part of ECMAScript 5th Edition and thus not implemented by all browsers (actually just IE8 and lower).

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
        "use strict";
        if (this === void 0 || 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 > 0) {
            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;
    }
}

本文标签: javascriptjQueryIE8Object doesn39t support this property or method How to fix thisStack Overflow