代码之家  ›  专栏  ›  技术社区  ›  user177800

如何让jquery.coach.app.js与IE8一起工作

  •  5
  • user177800  · 技术社区  · 15 年前

    我已经在IE7和IE8中的Windows XP SP3(在所有兼容模式下)和IE8中的Windows 7 Ultimate(在所有兼容模式下)上测试过这一点,但两者都失败了。我正在运行 couchapp 储存库。这在我的OSX 10.6.3开发机器上工作正常。我已经在Windows7Ultimate上测试了Chrome4.1.249.1064(45376)和Firefox3.6,它们都可以正常工作。与OSX 10.6.3上的Safari 4和Firefox 3.6一样

    这是错误信息

    网页错误详细信息

    用户代理:mozilla/4.0(兼容; msie 8.0;Windows NT 6.1;Trident/4.0; slcc2;.net clr 2.0.50727;.net clr 3.5.30729;.NET CLR 3.0.30729;Media Center PC 6.0)时间戳:周三,4月28日 2010年03:32:55 UTC

    消息:对象不支持此 属性或方法行:159字符:7 代码:0 URI: http://192.168.0.105:5984/test/_design/test/vendor/couchapp/jquery.couch.app.js

    下面是“冒犯性”代码,它在Chrome、Firefox和Safari上工作得很好。如果说失败在开始的线上 qs.forEach() 从文件中 jquery.couch.app.js

      157 var qs = document.location.search.replace(/^\?/,'').split('&');
      158 var q = {};
      159 qs.forEach(function(param) {
      160   var ps = param.split('=');
      161   var k = decodeURIComponent(ps[0]);
      162   var v = decodeURIComponent(ps[1]);
      163    if (["startkey", "endkey", "key"].indexOf(k) != -1) {
      164     q[k] = JSON.parse(v);
      165   } else {
      166    q[k] = v;
      167   }
      168 });
    
    2 回复  |  直到 15 年前
        1
  •  6
  •   edwin    15 年前

    foreach()是最近添加到javascript规范中的函数,因此并非所有浏览器都支持它。

    您可以在MDC上阅读: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/forEach

    在“兼容性”下,您将找到一个使foreach()可用的代码段。

    if (!Array.prototype.forEach)
    {
      Array.prototype.forEach = function(fun /*, thisp*/)
      {
        var len = this.length >>> 0;
        if (typeof fun != "function")
          throw new TypeError();
    
        var thisp = arguments[1];
        for (var i = 0; i < len; i++)
        {
          if (i in this)
            fun.call(thisp, this[i], i, this);
        }
      };
    }
    

    因此,将上面的代码复制并粘贴到脚本中,foreach()应该可以工作。

        2
  •  0
  •   user177800    15 年前

    我还必须补充 indexOf() 到数组对象,以便在修复foreach()问题后使其正常工作

    if (!Array.prototype.indexOf)
    {
        Array.prototype.indexOf = function(elt)
        {
            var len = this.length >>> 0;
    
            var from = Number(arguments[1]) || 0;
            from = (from < 0)
                    ? Math.ceil(from)
                    : Math.floor(from);
            if (from < 0)
                from += len;
    
            for (; from < len; from++)
            {
                if (from in this &&
                    this[from] === elt)
                    return from;
            }
            return -1;
        };
    }
    
    推荐文章