转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
兼容低版本 IE 的 JScript 5.5 实现         ★★★★

兼容低版本 IE 的 JScript 5.5 实现

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2992 更新时间:2009/4/23 11:27:39

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 方法实际上并没有考虑本地字符集,但在大部分系统上它还是工作正常的。

完全没有实现的:

  • 正则表达式对象中扩充的属性和限定符

.
———————————————–
如果你想测试低版本的 IE 浏览器上的效果,又没有安装低版本的 IE 浏览器,可以使用这个包:ie_all.zip。这里面都是不需要安装的 IE,直接释放压缩包到一个目录下,就可以运行了。如果你是 win9x 系统,可以到这里下载 win9x 版的 standalone IE。

/* iecompat.js
*
* Copyright Ma Bingyao <andot@ujn.edu.cn>
* Version: 1.1
* LastModified: 2006-02-10
* 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.desc

[1] [2] [3] [4] [5]  下一页


[Web开发]GridView根据值的变化改变行列样式  [Web开发]DataGrid或DataView进行求合
[Web开发]asp.net如何使用cookie(创建、保存、读取)  [Web开发]Grid或GridView分页数据不足,如何显示空行
[Web开发]设置GridView控件表头背景图片(前台设置)  [Web开发]设置GridView控件表头背景图片
[Web开发]Net读取(上传的)Excel内容显示到GridView示例源代…  [Web开发]用代码实现ReportViewer的导出功能
[Web开发]长篇大论—图文解说DridView、DataList、DetailsV…  [Web开发]ASP.NET2.0中Gridview中数据控件的操作技巧下篇
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台