开发者

how to get jquery.couch.app.js to work with IE8

I have tested this on Windows XP SP3 in IE7 and IE8 ( in all compatibility modes ) and Windows 7 Ultimate in IE8 (in all compatiblity modes) and it fails the same way on both. I am running the latest HEAD from the the couchapp repository. This works fine on my OSX 10.6.3 development machine. I have tested with C开发者_StackOverflowhrome 4.1.249.1064 (45376) and Firefox 3.6 on Windows 7 Ultimate and they both work fine. As do both Safari 4 and Firefox 3.6 on OSX 10.6.3

Here is the error message

Webpage error details

User Agent: Mozilla/4.0 (compatible; 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) Timestamp: Wed, 28 Apr 2010 03:32:55 UTC

Message: Object doesn't support this property or method Line: 159 Char: 7 Code: 0 URI: http://192.168.0.105:5984/test/_design/test/vendor/couchapp/jquery.couch.app.js

and here is the "offending" bit of code, which works on Chrome, Firefox and Safari just fine. If says the failure is on the line that starts qs.forEach() from the file 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 });


forEach() is a function that's recently added to the JavaScript specification, so not all browsers support it.

You can read about it at MDC: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/forEach

Under "Compatibility", you'll find a snippet that makes forEach() available.

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

So, copy and paste the code above to your script and forEach() should work.


I also had to add indexOf() to the Array object to get it working after fixing the forEach() problem

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜