兼容低版本IE的JScript5.5实现
兼容低版本IE的JScript5.5实现
发布时间:2016-12-30 来源:查字典编辑
摘要:作者Blog:www.coolcode.cnIE5.5中的JScript版本是5.5版,它比以前版本的JScript中多了如数组的push、...

作者Blog:www.coolcode.cn

IE5.5中的JScript版本是5.5版,它比以前版本的JScript中多了如数组的push、pop、shift、unshift方法和encodeURI、decodeURI等一些重要的函数。而这些增加的内容在目前其他浏览器(如Moziila/Firefox和Opera)上也同样支持。因此目前开发网站一般对于IE浏览器只能兼容到5.5版,而对于更低版本的IE(如IE5、IE4等),则不再去考虑了。虽然这些低版本的IE浏览器目前已经不是主流,但如果能够不需要修改现有代码就能够兼容它们的话,倒是也可以考虑。因此我做了这个兼容低版本IE的JScript5.5实现。当然它不可能完全兼容JScript5.5,但对于最常用的一些方法,都已经实现了。

该库使用非常简单,只需要在网页的head部分加入:

<scripttype="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方法

对于错误处理,IE5(JScript5)中已经有了try…catch和throw语句,因此decodeURI、decodeURIComponent、toExponential、toFixed、toPrecision、apply如果出现运行期错误,在IE5上会抛出跟IE5.5+中一样的错误信息,但是因为IE4没有错误处理语句,如果上述函数出现运行期错误,将会返回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方法

因为IE4不具备错误处理语句,因此Error对象在IE4上并不具备IE5以上Error对象应具有的功能,因此它对于IE4的实现只能保证你在访问或创建它时不会出错。

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 < 5) function Error(number, description) { if (!number) this.number = 0; else this.number = number; if (!description) this.description = ""; else this.description = description; } @end @*/ /*@if (@_jscript_version < 5.5) // this return value was not very correct Object.prototype.isPrototypeOf = function (o) { return (this.constructor == o.constructor); } // this return value was not very correct Object.prototype.hasOwnProperty = function (proName) { return (typeof(eval("this." + proName)) != "undefind"); } Object.prototype.propertyIsEnumerable = function (proName) { for (var o in this) { if ((proName == o.toString()) && (proName != "propertyIsEnumerable") && (proName != "isPrototypeOf") && (proName != "hasOwnProperty")) return true; } return false; } Error.prototype.message = ""; Error.prototype.name = "Error"; Date.prototype.toDateString = function () { var s = this.toString().split(' '); return [s[0], s[1], s[2], s[5]].join(' '); } Date.prototype.toTimeString = function () { var s = this.toString().split(' '); return [s[3], s[4]].join(' '); } Date.prototype.toLocaleDateString = function () { return this.toLocaleString().split(' ')[0]; } Date.prototype.toLocaleTimeString = function () { return this.toLocaleString().split(' ')[1]; } String.prototype.toLocaleLowerCase = function () { return this.toLowerCase(); } String.prototype.toLocaleUpperCase = function () { return this.toUpperCase(); } String.prototype.localeCompare = function (str) { if (this > str) return 1; if (this < str) return -1; return 0; } Number.prototype.toExponential = function (n) { function repeat(s, n) { var out = ""; for (var i = 0; i < n; i++) { out += s; } return out; } if (!n) { n = 0; } else { n = parseInt(n); if (n < 0 || n > 20) { @if (@_jscript_version < 5) return null; @else var e = new Error(-2146823262, "The number of fractional digits is out of range"); e.name = "RangeError"; e.message = e.description; throw(e); @end } } var s, d, e, len; s = this.toString().split("e"); d = parseFloat(s[0]); e = 0; if (typeof(s[1]) != "undefined") { e = parseInt(s[1]); } s = d.toString().split("."); if (typeof(s[1]) != "undefined") { e = e - s[1].length; d = s[0] + s[1]; d = d.replace(/^0+/g, ""); if (d == "") d = "0"; } s = d.toString(); len = s.length - 1; e += len; if (len < n) { s += repeat("0", n - len); } else if (len > 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 < n; i++) { out += s; } return out; } if (!n) { n = 0; } else { n = parseInt(n); if (n < 0 || n > 20) { @if (@_jscript_version < 5) return null; @else var e = new Error(-2146823262, "The number of fractional digits is out of range"); e.name = "RangeError"; e.message = e.description; throw(e); @end } } var s, d, e, len; s = this.toString().split("e"); d = parseFloat(s[0]); e = 0; if (typeof(s[1]) != "undefined") { e = parseInt(s[1]); } s = d.toString().split("."); if (typeof(s[1]) != "undefined") { e = e - s[1].length; d = s[0] + s[1]; d = d.replace(/^0+/g, ""); if (d == "") d = "0"; } s = d.toString(); len = s.length - 1; if (e >= 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 < n; i++) { out += s; } return out; } if (typeof(n) == "undefined") { return this.toString(); } else { n = parseInt(n); if (n < 1 || n > 21) { @if (@_jscript_version < 5) return null; @else var e = new Error(-2146823262, "The precision is out of range"); e.name = "RangeError"; e.message = e.description; throw(e); @end } } if (this.valueOf() == 0) { if (n == 1) return "0"; return "0." + repeat("0", n - 1); } var s, d, e, len; s = this.toString().split("e"); d = parseFloat(s[0]); e = 0; if (typeof(s[1]) != "undefined") { e = parseInt(s[1]); } s = d.toString().split("."); if (typeof(s[1]) != "undefined") { e = e - s[1].length; d = s[0] + s[1]; d = d.replace(/^0+/g, ""); if (d == "") d = "0"; } s = d.toString(); len = s.length; d = parseFloat("." + s) * Math.pow(10, n); s = Math.round(d).toString(); if (s.length > parseInt(d).toString().length) { e++; s = s.slice(0, -1); } e += len - s.length; len = s.length; e += len - 1; if (e < n && e > -7) { if ((e < n - 1) && (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 < len; i++) { c = str.charCodeAt(i); if (c 6) & 0x1F)).toString(16).toUpperCase(); out[j++] = '%' + (0x80 | ( c & 0x3F)).toString(16).toUpperCase(); continue; } else if (c < 0xD800 || 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 < len) { c2 = str.charCodeAt(i); if (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 < 5) return null; @else var e = new Error(-2146823264, "The URI to be encoded contains an invalid character"); e.name = "URIError"; e.message = e.description; throw(e); @end } return out.join(''); } function encodeURIComponent(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', '%23', '%24', '%25', '%26', "'", '(', ')', '*', '%2B', '%2C', '-', '.', '%2F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '%3A', '%3B', '%3C', '%3D', '%3E', '%3F', '%40', '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; out = []; len = str.length; for(i = 0, j = 0; i < len; i++) { c = str.charCodeAt(i); if (c 6) & 0x1F)).toString(16).toUpperCase(); out[j++] = '%' + (0x80 | ( c & 0x3F)).toString(16).toUpperCase(); continue; } else if (c < 0xD800 || 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 < len) { c2 = str.charCodeAt(i); if (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 < 5) return null; @else var e = new Error(-2146823264, "The URI to be encoded contains an invalid character"); e.name = "URIError"; e.message = e.description; throw(e); @end } return out.join(''); } function decodeURI(str) { function throwerror() { @if (@_jscript_version < 5) return null; @else var e = new Error(-2146823263, "The URI to be decoded is not a valid encoding"); e.name = "URIError"; e.message = e.description; throw(e); @end } function checkcode() { var d1, d2; d1 = str.charAt(i++); d2 = str.charAt(i++); if (isNaN(parseInt(d1, 16)) || isNaN(parseInt(d2, 16))) { return throwerror(); } return parseInt(d1 + d2, 16); } function checkutf8() { var c = str.charCodeAt(i++); if (c == 37) { if ((c = checkcode()) == null) return null; } if ((c >> 6) != 2) { return throwerror(); } } var out, i, j, len; var c, c2, c3, c4, s; out = []; len = str.length; i = j = 0; while(i < len) { c = str.charCodeAt(i++); if (c == 37) { if ((c = checkcode()) == null) return null; } else { out[j++] = String.fromCharCode(c); continue; } switch(c) { case 35: case 36: case 38: case 43: case 44: case 47: case 58: case 59: case 61: case 63: case 64: { if (str.charCodeAt(i - 3) == 37) { out[j++] = str.substr(i - 3, 3); } else { out[j++] = str.substr(i - 1, 1); } break; } default: { switch (c >> 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)

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新Javascript教程学习
热门Javascript教程学习
编程开发子分类