兼容低版本IE的JScript5.5实现
作者Blog:www.coolcode.cn
IE 5.5 中的 JScript 版本是 5.5 版,它比以前版本的 JScript 中多了如数组的 push、pop、shift、unshift 方法和 encodeURI、decodeURI 等一些重要的函数。而这些增加的内容在目前其他浏览器(如 Moziila/Firefox 和 Opera)上也同样支持。因此目前开发网站一般对于 IE 浏览器只能兼容到 5.5 版,而对于更低版本的 IE(如 IE 5、IE 4 等),则不再去考虑了。虽然这些低版本的 IE 浏览器目前已经不是主流,但如果能够不需要修改现有代码就能够兼容它们的话,倒是也可以考虑。因此我做了这个兼容低版本 IE 的 JScript 5.5 实现。当然它不可能完全兼容 JScript 5.5,但对于最常用的一些方法,都已经实现了。
该库使用非常简单,只需要在网页的 head 部分加入:
<script type="text/javascript" src="iecompat.js"></script> 
就可以了。
完全实现的:
Array 对象中:
·push 方法 
·pop 方法 
·shift 方法 
·unshift 方法 
·splice 方法
Date 对象中:
·toDateString 方法 
·toTimeString 方法 
·toLocaleDateString 方法 
·toLocaleTimeString 方法 
·Function 对象中:  
·apply 方法 
·call 方法
Global 对象中:
·undefined 属性 
·encodeURI 方法 
·encodeURIComponent 方法 
·decodeURI 方法 
·decodeURIComponent 方法
Number 对象中:
·toExponential 方法 
·toFixed 方法 
·toPrecision 方法
对于错误处理,IE 5(JScript 5)中已经有了 try…catch 和 throw 语句,因此 decodeURI、decodeURIComponent、toExponential、toFixed、toPrecision、apply 如果出现运行期错误,在 IE 5 上会抛出跟 IE 5.5+ 中一样的错误信息,但是因为 IE 4 没有错误处理语句,如果上述函数出现运行期错误,将会返回 null。注意上面说的运行期错误,不是指上述函数实现中的错误,而是指在这些函数正常工作的情况下应该出现的错误。
其中 Function 的 apply 函数的实现参考了: 
http://www.openjsan.org/doc/a/ad/adamk/Upgrade/0.04/lib/Upgrade/Function/apply.html 
这段程序。
不完全实现的:
Error 对象
Object 对象中:
·isPrototypeOf 方法 
·hasOwnProperty 方法 
·propertyIsEnumerable 方法
String 对象中:
·toLocaleLowerCase 方法 
·toLocaleUpperCase 方法 
·localeCompare 方法
因为 IE 4 不具备错误处理语句,因此 Error 对象在 IE 4 上并不具备 IE 5 以上 Error 对象应具有的功能,因此它对于 IE 4 的实现只能保证你在访问或创建它时不会出错。
Object 中的 isPrototypeOf、hasOwnProperty 和 propertyIsEnumerable 方法只是做了模拟实现,其返回值并非总是正确。
String 对象中的 toLocaleLowerCase、toLocaleUpperCase 和 localeCompare 方法实际上并没有考虑本地字符集,但在大部分系统上它还是工作正常的。
完全没有实现的:
正则表达式对象中扩充的属性和限定符
下载: iecompat.js
/* iecompat.js
*
* Copyright Ma Bingyao
* Version: 1.3
* LastModified: 2006-06-18
* This library is free.  You can redistribute it and/or modify it.
* http://www.coolcode.cn/?p=126
*/
/*@cc_on @*/
/*@if (@_jscript_version  str) return 1;
    if (this  20) {
            @if (@_jscript_version  n) {
        s = Math.round(parseFloat("." + s) * Math.pow(10, n + 1)).toString();
        if ((s.length - 1) > n) {
            e += 1;
            s = Math.round(parseFloat("." + s) * Math.pow(10, n + 1)).toString();
        }
    }
    if (e >= 0) {
        e = "+" + e;
    }
    if (n == 0) {
        return s + "e" + e;
    }
    else {
        return s.substr(0, 1) + "." + s.substr(1) + "e" + e;
    }
}
Number.prototype.toFixed = function (n) {
    function repeat(s, n) {
        var out = "";
        for (var i = 0; i  20) {
            @if (@_jscript_version = 0) {
        s += repeat("0", e);
        if (n > 0) {
            s += "." + repeat("0", n);
        }
    }
    else if (-e  0) {
            s = repeat("0", n - s.length + 1) + s;
            s = s.substr(0, s.length - n) + "." + s.substr(s.length - n);
        }
    }
    return s;
}
Number.prototype.toPrecision = function (n) {
    function repeat(s, n) {
        var out = "";
        for (var i = 0; i  21) {
            @if (@_jscript_version  parseInt(d).toString().length) {
        e++;
        s = s.slice(0, -1);
    }
    e += len - s.length;
    len = s.length;
    e += len - 1;
if (e  -7) {
        if ((e  0)) {
            s = s.substr(0, e + 1) + "." + s.substr(e + 1);
        }
        else if (e = 0) {
            e = "+" + e;
        }
        if (n == 1) {
            s += "e" + e;
        }
        else {
            s = s.substr(0, 1) + "." + s.substr(1) + "e" + e;
        }
    }
    return s;
}
var undefined;
function encodeURI(str) {
    var l = ['%00', '%01', '%02', '%03', '%04', '%05', '%06',
             '%07', '%08', '%09', '%0A', '%0B', '%0C', '%0D',
             '%0E', '%0F', '%10', '%11', '%12', '%13', '%14',
             '%15', '%16', '%17', '%18', '%19', '%1A', '%1B',
             '%1C', '%1D', '%1E', '%1F', '%20', '!', '%22',
             '#', '$', '%25', '&', "'", '(', ')', '*', '+', ',',
             '-', '.', '/', '0', '1', '2', '3', '4', '5', '6',
             '7', '8', '9', ':', ';', '%3C', '=', '%3E', '?',
             '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
             'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
             'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '%5B', '%5C',
             '%5D', '%5E', '_', '%60', 'a', 'b', 'c', 'd', 'e',
             'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
             'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
             'z', '%7B', '%7C', '%7D', '~', '%7F'];
    var out, i, j, len, c, c2;
out = [];
    len = str.length;
    for (i = 0, j = 0; i >  6) & 0x1F)).toString(16).toUpperCase();
            out[j++] = '%' + (0x80 | (        c & 0x3F)).toString(16).toUpperCase();
            continue;
        }
        else if (c  0xDFFF) {
            out[j++] = '%' + (0xE0 | ((c >> 12) & 0x0F)).toString(16).toUpperCase();
            out[j++] = '%' + (0x80 | ((c >>  6) & 0x3F)).toString(16).toUpperCase();
            out[j++] = '%' + (0x80 |         (c & 0x3F)).toString(16).toUpperCase();
            continue;
        }
        else {
            if (++i >> 18) & 0x3F)).toString(16).toUpperCase();
                        out[j++] = '%' + (0x80 | ((c >>> 12) & 0x3F)).toString(16).toUpperCase();
                        out[j++] = '%' + (0x80 | ((c >>>  6) & 0x3F)).toString(16).toUpperCase();
                        out[j++] = '%' + (0x80 |          (c & 0x3F)).toString(16).toUpperCase();
                        continue;
                    }
                }
            }
        }
        @if (@_jscript_version >  6) & 0x1F)).toString(16).toUpperCase();
            out[j++] = '%' + (0x80 | (        c & 0x3F)).toString(16).toUpperCase();
            continue;
        }
        else if (c  0xDFFF) {
            out[j++] = '%' + (0xE0 | ((c >> 12) & 0x0F)).toString(16).toUpperCase();
            out[j++] = '%' + (0x80 | ((c >>  6) & 0x3F)).toString(16).toUpperCase();
            out[j++] = '%' + (0x80 |         (c & 0x3F)).toString(16).toUpperCase();
            continue;
        }
        else {
            if (++i >> 18) & 0x3F)).toString(16).toUpperCase();
                        out[j++] = '%' + (0x80 | ((c >>> 12) & 0x3F)).toString(16).toUpperCase();
                        out[j++] = '%' + (0x80 | ((c >>>  6) & 0x3F)).toString(16).toUpperCase();
                        out[j++] = '%' + (0x80 |          (c & 0x3F)).toString(16).toUpperCase();
                        continue;
                    }
                }
            }
        }
        @if (@_jscript_version > 6) != 2) {
            return throwerror();
        }
    }
    var out, i, j, len;
    var c, c2, c3, c4, s;
out = [];
    len = str.length;
    i = j = 0;
    while(i > 4) {
                    case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
                        // 0xxxxxxx
                        out[j++] = String.fromCharCode(c);
                        break;
                    }
                    case 12: case 13: {
                        // 110x xxxx   10xx xxxx
                        if ((c2 = checkutf8()) == null) return null;
                        out[j++] = String.fromCharCode(((c & 0x1F) >> 10) & 0x03ff) | 0xd800,
                                                                    (s         & 0x03ff) | 0xdc00);
                                }
                                else {
                                    return throwerror();
                                }
                                break;
                            }
                            default: {
                                return throwerror();
                            }
                        }
                    }
                }
            }
        }
    }
    return out.join('');
}
function decodeURIComponent(str) {
    function throwerror() {
        @if (@_jscript_version > 6) != 2) {
            return throwerror();
        }
    }
    var out, i, j, len;
    var c, c2, c3, c4, s;
out = [];
    len = str.length;
    i = j = 0;
    while(i > 4) {
            case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
                // 0xxxxxxx
                out[j++] = String.fromCharCode(c);
                break;
            }
            case 12: case 13: {
                // 110x xxxx   10xx xxxx
                if ((c2 = checkutf8()) == null) return null;
                out[j++] = String.fromCharCode(((c & 0x1F) >> 10) & 0x03ff) | 0xd800,
                                                            (s         & 0x03ff) | 0xdc00);
                        }
                        else {
                            return throwerror();
                        }
                        break;
                    }
                    default: {
                        return throwerror();
                    }
                }
            }
        }
    }
    return out.join('');
}
Array.prototype.push = function () {
    var curlen = this.length;
    for (var i = 0; i = 0 ; i--) {
        this[i + arglen] = this[i] ;
    }
    for (var i = 0; i  1) {
        for (var i = 1; i 
  [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
如果你想测试低版本的 IE 浏览器上的效果,又没有安装低版本的 IE 浏览器,可以使用这个包:ie_all.zip 。这里面都是不需要安装的 IE,直接释放压缩包到一个目录下,就可以运行了。如果你是 win9x 系统,可以到 这里 下载 win9x 版的 standalone IE。
