兼容低版本IE的JScript5.5实现_javascript技巧_脚本之家

作者Blog:www.coolcode.cn
IE5.5中的JScript版本是5.5版,它比以前版本的JScript中多了如数组的push、pop、shift、unshift方法和encodeUPRADOI、decodeUQX56I等片段器重的函数。而这几个充实的内容在当下任何浏览器(如Moziila/Firefox和Opera)上也相近支持。由此如今成本网址平时对于IE浏览器只好合作到5.5版,而对于更低版本的IE,则不再去思谋了。固然这个低版本的IE浏览器最近早就不是主流,但只要能够无需改善现存代码就能够合作它们来讲,倒是也得以虚构。因而作者做了这些包容低版本IE的JScript5.5实现。当然它不容许完全包容JScript5.5,但对此最常用的一部分办法,都曾经落成了。
该库使用特别轻便,只供给在网页的head部分参加: 就足以了。 完全达成的:
Array对象中: ·push方法 ·pop方法 ·shift方法 ·unshift方法 ·splice方法
Date对象中: ·toDateString方法 ·toTimeString方法 ·toLocaleDateString方法
·toLocaleTimeString方法 ·Function对象中: ·apply方法 ·call方法
Global对象中: ·undefined属性 ·encodeU奥迪Q7I方法 ·encodeUHavalIComponent方法
·decodeUKoleosI方法 ·decodeU帕杰罗IComponent方法 Number对象中: ·toExponential方法
·toFixed方法 ·toPrecision方法
对于错误管理,IE5中已经有了try…catch和throw语句,由此decodeU大切诺基I、decodeUKoleosIComponent、toExponential、toFixed、toPrecision、apply假使现身运转期错误,在IE5上会抛出跟IE5.5+中一成不改变的错误音信,可是因为IE4对的误管理语句,假使上述函数现身运转期错误,将会回来null。注意上面说的运作期错误,不是指上述函数完毕中的错误,而是指在这里些函数符合规律办事的事态下应当现身的荒诞。
此中Function的apply函数的兑现参照他事他说加以考察了:

这段程序。 不完全贯彻的: 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. * */ /*@cc_on
@*/ /*@if (@_jscript_version < 5) function Error { if this.number
= 0; else this.number = number; if this.description = “”; else
this.description = description; } @end @*/ /*@if (@_jscript_version
< 5.5) // this return value was not very correct
Object.prototype.isPrototypeOf = function { return (this.constructor ==
o.constructor); } // this return value was not very correct
Object.prototype.hasOwnProperty = function { return (typeof(eval !=
“undefind”); } Object.prototype.propertyIsEnumerable = function { for {
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; return [s[0], s[1], s[2],
s[5]].join; } Date.prototype.toTimeString = function () { var s =
this.toString; return [s[3], s[4]].join; }
Date.prototype.toLocaleDateString = function () { return
this.toLocaleString[0]; } Date.prototype.toLocaleTimeString = function
() { return this.toLocaleString[1]; }
String.prototype.toLocaleLowerCase = function () { return
this.toLowerCase(); } String.prototype.toLocaleUpperCase = function () {
return this.toUpperCase(); } String.prototype.localeCompare = function {
if return 1; if return -1; return 0; } Number.prototype.toExponential =
function { function repeat { var out = “”; for (var i = 0; i < n;
i++) { out += s; } return out; } if { n = 0; } else { n = parseInt; if {
@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; @end } } var s,
d, e, len; s = this.toString; d = parseFloat; e = 0; if != “undefined”)
{ e = parseInt; } s = d.toString; if != “undefined”) { e = e –
s[1].length; d = s[0] + s[1]; d = d.replace; if d = “0”; } s =
d.toString(); len = s.length – 1; e += len; if { s += repeat; } else if
{ s = Math.round * Math.pow.toString > n) { e += 1; s = Math.round
* Math.pow.toString { e = “+” + e; } if { return s + “e” + e; } else {
return s.substr + “.” + s.substr + “e” + e; } } Number.prototype.toFixed
= function { function repeat { var out = “”; for (var i = 0; i < n;
i++) { out += s; } return out; } if { n = 0; } else { n = parseInt; if {
@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; @end } } var s,
d, e, len; s = this.toString; d = parseFloat; e = 0; if != “undefined”)
{ e = parseInt; } s = d.toString; if != “undefined”) { e = e –
s[1].length; d = s[0] + s[1]; d = d.replace; if d = “0”; } s =
d.toString(); len = s.length – 1; if { s += repeat { s += “.” + repeat;
} } else if { s = repeat(“0”, 1 – e – s.length) + s; s = s.substr + “.”

  • s.substr + repeat; } else { s = repeat(“0”, 1 – e – s.length) + s; d =
    parseFloat(s.substr + “.” + s.substr; s = Math.round.toString { s =
    repeat(“0”, n – s.length + 1) + s; s = s.substr + “.” + s.substr; } }
    return s; } Number.prototype.toPrecision = function { function repeat {
    var out = “”; for (var i = 0; i < n; i++) { out += s; } return out; }
    if == “undefined”) { return this.toString(); } else { n = parseInt; if {
    @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; @end } } if { if return
    “0”; return “0.” + repeat; } var s, d, e, len; s = this.toString; d =
    parseFloat; e = 0; if != “undefined”) { e = parseInt; } s = d.toString;
    if != “undefined”) { e = e – s[1].length; d = s[0] + s[1]; d =
    d.replace; if d = “0”; } s = d.toString(); len = s.length; d =
    parseFloat * Math.pow; s = Math.round; if (s.length > parseInt {
    e++; s = s.slice; } e += len – s.length; len = s.length; e += len – 1;
    if { if && { s = s.substr + “.” + s.substr; } else if { s = repeat + s;
    s = s.substr + “.” + s.substr; } } else { if { s += repeat; } if { e =
    “+” + e; } if { s += “e” + e; } else { s = s.substr + “.” + s.substr +
    “e” + e; } } return s; } var undefined; function encodeURI { 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 {
    out[j++] = l[c]; continue; } else if { out[j++] = ‘%’ + &
    0x1F)).toString; out[j++] = ‘%’ + .toString; continue; } else if (c
    < 0xD800 || c > 0xDFFF) { out[j++] = ‘%’ + & 0x0F)).toString;
    out[j++] = ‘%’ + & 0x3F)).toString; out[j++] = ‘%’ + .toString;
    continue; } else { if { c2 = str.charCodeAt; if (c <= 0xDBFF &&
    0xDC00 <= c2 && c2 <= 0xDFFF) { c = << 10 | + 0x010000; if
    (0x010000 <= c && c <= 0x10FFFF) { out[j++] = ‘%’ + &
    0x3F)).toString; out[j++] = ‘%’ + & 0x3F)).toString; out[j++] = ‘%’
  • & 0x3F)).toString; out[j++] = ‘%’ + .toString; 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; @end
    } return out.join; } function encodeURIComponent { 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 { out[j++] = l[c]; continue; } else if { out[j++] =
    ‘%’ + & 0x1F)).toString; out[j++] = ‘%’ + .toString; continue; } else
    if (c < 0xD800 || c > 0xDFFF) { out[j++] = ‘%’ + &
    0x0F)).toString; out[j++] = ‘%’ + & 0x3F)).toString; out[j++] = ‘%’
  • .toString; continue; } else { if { c2 = str.charCodeAt; if (c <=
    0xDBFF && 0xDC00 <= c2 && c2 <= 0xDFFF) { c = << 10 | +
    0x010000; if (0x010000 <= c && c <= 0x10FFFF) { out[j++] = ‘%’ +
    & 0x3F)).toString; out[j++] = ‘%’ + & 0x3F)).toString; out[j++] =
    ‘%’ + & 0x3F)).toString; out[j++] = ‘%’ + .toString; 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; @end
    } return out.join; } function decodeURI { 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; @end } function
    checkcode() { var d1, d2; d1 = str.charAt; d2 = str.charAt; if (isNaN ||
    isNaN { return throwerror(); } return parseInt; } function checkutf8() {
    var c = str.charCodeAt { if == null) return null; } if { return
    throwerror(); } } var out, i, j, len; var c, c2, c3, c4, s; out = [];
    len = str.length; i = j = 0; while { c = str.charCodeAt { if == null)
    return null; } else { out[j++] = String.fromCharCode; continue; }
    switch { case 35: case 36: case 38: case 43: case 44: case 47: case 58:
    case 59: case 61: case 63: case 64: { if == 37) { out[j++] =
    str.substr; } else { out[j++] = str.substr; } break; } default: {
    switch { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
    { // 0xxxxxxx out[j++] = String.fromCharCode; break; } case 12: case
    13: { // 110x xxxx 10xx xxxx if == null) return null; out[j++] =
    String.fromCharCode | ; break; } case 14: { // 1110 xxxx 10xx xxxx 10xx
    xxxx if == null) return null; if == null) return null; out[j++] =
    String.fromCharCode | | ; break; } default: { switch { case 0: case 1:
    case 2: case 3: case 4: case 5: case 6: case 7: { // 1111 0xxx 10xx xxxx
    10xx xxxx 10xx xxxx if == null) return null; if == null) return null; if
    == null) return null; s = | | | – 0x10000; if (0 <= s && s <=
    0xfffff) { out[j++] = String.fromCharCode | 0xd800, ; } else { return
    throwerror(); } break; } default: { return throwerror(); } } } } } } }
    return out.join; } function decodeURIComponent { 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; @end } function
    checkcode() { var d1, d2; d1 = str.charAt; d2 = str.charAt; if (isNaN ||
    isNaN { return throwerror(); } return parseInt; } function checkutf8() {
    var c = str.charCodeAt { if == null) return null; } if { return
    throwerror(); } } var out, i, j, len; var c, c2, c3, c4, s; out = [];
    len = str.length; i = j = 0; while { c = str.charCodeAt { if == null)
    return null; } else { out[j++] = String.fromCharCode; continue; }
    switch { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
    { // 0xxxxxxx out[j++] = String.fromCharCode; break; } case 12: case
    13: { // 110x xxxx 10xx xxxx if == null) return null; out[j++] =
    String.fromCharCode | ; break; } case 14: { // 1110 xxxx 10xx xxxx 10xx
    xxxx if == null) return null; if == null) return null; out[j++] =
    String.fromCharCode | | ; break; } default: { switch { case 0: case 1:
    case 2: case 3: case 4: case 5: case 6: case 7: { // 1111 0xxx 10xx xxxx
    10xx xxxx 10xx xxxx if == null) return null; if == null) return null; if
    == null) return null; s = | | | – 0x10000; if (0 <= s && s <=
    0xfffff) { out[j++] = String.fromCharCode | 0xd800, ; } else { return
    throwerror(); } break; } default: { return throwerror(); } } } } }
    return out.join; } Array.prototype.push = function () { var curlen =
    this.length; for (var i = 0; i < arguments.length; i++) {
    this[curlen + i] = arguments[i]; } return this.length; }
    Array.prototype.pop = function () { var returnValue = this[this.length
  • 1]; this.length–; return returnValue; } Array.prototype.shift =
    function () { var returnValue = this[0]; for (var i = 1; i <
    this.length; i++) { this[i – 1] = this[i]; } this.length–; return
    returnValue; } Array.prototype.unshift = function () { var curlen =
    this.length; var arglen = arguments.length; for (var i = curlen – 1; i
    >= 0 ; i–) { this[i + arglen] = this[i] ; } for (var i = 0; i
    < arglen; i++) { this[永利皇宫463娱乐网址,i] = arguments[i]; } //return this.length;
    // return this value is Gecko/Opera implementation. but IE don’t return
    anything. } Array.prototype.splice = function () { var start =
    arguments[0]; var deleteCount = arguments[1]; var len =
    arguments.length – 2; var returnValue = this.slice; for (var i = 0; i
    < len; i++) { this[start + i] = arguments[i + 2]; } for (var i =
    0; i < returnValue.length – deleteCount; i++) { this[start + len +
    i] = returnValue[deleteCount + i]; } this.length = start + len +
    returnValue.length – deleteCount; returnValue.length = deleteCount;
    return returnValue; } Function.prototype.apply = function { var ps =
    []; if == “undefined”) { var __$$apply$$__ = this; return
    eval(‘__$$apply$$__ + ‘);’); } if == “undefined”) { @if
    (@_jscript_version < 5) return null; @else var e = new
    Error(-2146823260, “Array or arguments object expected”); e.name =
    “TypeError”; e.message = e.description; throw; @end } for (var i = 0; i
    < p.length; i++) { ps[i] = ‘p[‘ + i + ‘]’; } } if == “object”) ||
    == “function”)) { o.__$$apply$$__ = this; var returnValue =
    eval(‘o.__$$apply$$__ + ‘);’); o.__$$apply$$__ = null; return
    returnValue; } else { this.__$$apply$$__ = this; var returnValue =
    eval(‘this.__$$apply$$__ + ‘);’); this.__$$apply$$__ = null;
    return returnValue; } } Function.prototype.call = function { var ps =
    []; if == “undefined”) { var __$$call$$__ = this; return
    eval(‘__$$call$$__ + ‘);’); } if { for (var i = 1; i <
    arguments.length; i++) { ps[i – 1] = ‘arguments[‘ + i + ‘]’; } } if
    == “object”) || == “function”)) { o.__$$call$$__ = this; var
    returnValue = eval(‘o.__$$call$$__ + ‘);’); o.__$$call$$__ =
    null; return returnValue; } else { this.__$$call$$__ = this; var
    returnValue = eval(‘this.__$$call$$__ + ‘);’); this.__$$call$$__
    = null; return returnValue; } } @end @*/

[Ctrl+A 全选 注:如需引进外界Js需刷新能力试行]只要你想测量检验低版本的 IE
浏览器上的功效,又还未有设置低版本的 IE
浏览器,能够运用那些包:ie_all.zip。那其间都以不供给设置的
IE,直接出狱压缩包到一个索引下,就能够运维了。假令你是 win9x
系统,能够到 这里下载 win9x 版的 standalone IE。

发表评论

电子邮件地址不会被公开。 必填项已用*标注