javascript - 简单 JSON 对象抛出 "Object doesn' t 支持此属性或方法”

标签 javascript json internet-explorer-8

我写了一个小framework for asynchronous resource loading (link) .在 Opera、Chrome、Firefox 等大多数现代浏览器中,它都按预期工作。

我在IE8下也试过了,但是代码不执行。 IE 在一个简单的格式良好的 [通过 eval() 检查] JSON 对象上抛出“对象不支持此属性或方法”。

我该如何解决这个问题或导致此错误的原因?

最佳答案

可能引发错误是因为 IE8 不支持 .indexOf() .

引用:

indexOf was added to the ECMA-262 standard in the 5th edition; as such it may not be present in all browsers. You can work around this by utilizing the following code at the beginning of your scripts. This will allow you to use indexOf when there is still no native support. This algorithm matches the one specified in ECMA-262, 5th edition, assuming TypeError and Math.abs have their original values.

使用 polyfill 代码:

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

关于javascript - 简单 JSON 对象抛出 "Object doesn' t 支持此属性或方法”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21546249/

相关文章:

php - 使用 json 显示组合图表

java - java中列表的情况下使用jackson json反序列化错误

javascript - IE8 的谷歌地图 .setMap 问题

css - Internet Explorer,突出显示文本时会更改字体和/或大小

javascript - RoR 渲染 PDF 并自动打印

javascript - 将另一个页面加载到 DIV 中

javascript - ASCII 以外的字符不能正确显示

javascript - JavaScript 或 TypeScript 中的条件链接

javascript - 使用 JSON 数据动态命名 dataTable 中的列

javascript - 导致 Internet Explorer 8 崩溃的简单网页