关于作者

完美Ajax类,支持事件

上一篇 / 下一篇  2007-01-04 11:29:51

查看( 1382 ) / 评论( 9 )

CODE:

<scrīpt Language="Javascrīpt">
<!--
// author:奔腾的心
//     qq:7180001

function Ajax(OnError,OnState,OnDownloadEnd)
{
        this.ErrorStr                 = null;
        this.OnError                 = OnError;
        this.OnState                 = OnState;
        this.OnDownloadEnd         = OnDownloadEnd;

        this.method                = "GET";
        this.URL                = null;
        this.sync                = true;
        this.PostData                = null
        this.RetData                 = null;

        this.HttpObj                 = this.createXMLHttpRequest();
        if(this.HttpObj == null)
        {
                return;
        }

        var ōbj = this;
        this.HttpObj.onreadystatechange = function()
        {
                Ajax.handleStateChange(Obj);
        }
}

Ajax.prototype.UTFTOGB = function(strBody)
{
        var Rec=new ActiveXObject("ADODB.RecordSet");
        Rec.Fields.Append("DDD",201,1);
        Rec.Open();
        Rec.AddNew();
        Rec(0).AppendChunk(strBody);
        Rec.Update();
        var HTML=Rec(0).Value;
        Rec.Close();
        delete Rec;
        return(HTML);
}

Ajax.prototype.createXMLHttpRequest = function()
{
        if (window.XMLHttpRequest)
        {
                //Mozilla 浏览器
                return new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        {
                var msxmls = new Array('Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP');
                for (var i = 0; i < msxmls.length; i++)
                {
                        try
                        {
                                return new ActiveXObject(msxmls[i]);
                        }catch (e){}

                }
        }
        this.ErrorStr = "你的浏览器不支持XMLHttpRequest对象."
        if(this.OnError)
        {
                this.OnError(this.ErrorStr);
        }
            return null;
}

Ajax.prototype.send = function()
{

        if (this.HttpObj !== null)
        {
                this.URL = this.URL + "?t=" + new Date().getTime();
                this.HttpObj.open(this.method, this.URL, this.sync);
                if(this.method.toLocaleUpperCase() == "GET")
                {
                        this.HttpObj.send(null);
                }
                else if(this.method.toLocaleUpperCase() == "POST")
                {
                        this.HttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                        this.HttpObj.send(this.PostData);
                }
                else
                {
                        this.ErrorStr = "错误的[method]命令."
                        if(this.OnError)
                        {
                                this.OnError(this.ErrorStr);
                        }
                        return;
                }

                if (this.HttpObj.readyState == 4)
                {
                        // 判断对象状态
                            if (this.HttpObj.status == 200)
                        {
                                this.RetData = this.UTFTOGB(this.HttpObj.responseBody);
                                if(this.OnDownloadEnd)
                                {
                                        this.OnDownloadEnd(this.RetData);
                                }
                                return;
                        }
                        else
                        {
                                this.ErrorStr = "您所请求的页面有异常."
                                if(this.OnError)
                                {
                                        this.OnError(this.ErrorStr);
                                }
                                return;
                        }
                }

        }

}

Ajax.handleStateChange = function(Obj)
{
        if(Obj.OnState)
        {
                Obj.OnState(Obj.HttpObj.readyState);
        }

        if (Obj.HttpObj.readyState == 4)
        {
                // 判断对象状态
                    if (Obj.HttpObj.status == 200)
                {
                        Obj.RetData = Obj.UTFTOGB(Obj.HttpObj.responseBody);
                        if(Obj.OnDownloadEnd)
                        {
                                Obj.OnDownloadEnd(Obj.RetData);
                        }
                        return;
                }
                else
                {
                        Obj.ErrorStr = "您所请求的页面有异常."
                        if(Obj.OnError)
                        {
                                Obj.OnError(Obj.ErrorStr);
                        }
                        return;
                }
        }
}


function EventError(strValue)
{
        Error.innerHTML = strValue;
}

function EventState(strValue)
{
        var strState = null;
        switch (strValue)
        {
                   case 0:
                strState = "未初始化...";
                break;

                   case 1:
                strState = "开始读取数据...";
                break;

                   case 2:
                strState = "读取数据...";
                break;

                   case 3:
                strState = "读取数据中...";
                break;

                   case 4:
                strState = "读取完成...";
                break;

                   default:
                strState = "未初始化...";
                break;
        }
        State.innerHTML = strState;
}

function EventDownloadEnd(strValue)
{
        DownloadEnd.innerHTML = strValue;
}


var A1 = new Ajax(EventError,EventState,EventDownloadEnd);
A1.method = "GET";
A1.URL = "http://bbs.51js.com"
A1.sync = true;
A1.send();
//-->
</scrīpt>

<div id="Error"></div>
<div id="State"></div>
<div id="DownloadEnd"></div>

TAG:

绿竹居发布于2007-01-04 11:27:19
这个看不懂,,刚刚学习。一样顶一下~~~
xinge的个人空间 xinge 发布于2007-01-04 11:28:19
最好写一点说明。
aasvvv发布于2007-01-04 11:35:28
不错,代码里面的那个function EventState(strValue)函数我刚好要用到
ivvn的个人空间 ivvn 发布于2007-01-04 11:49:58

QUOTE:

原帖由 aasvvv 于 2007-1-4 11:35 发表
不错,代码里面的那个function EventState(strValue)函数我刚好要用到
你论坛的页面加载用的什么方法,效果不错
tugang发布于2007-01-04 13:57:27
好像不支持FF呀.
Rimifon的个人空间 Rimifon 发布于2007-01-04 14:04:43
209行错误:State未定义……
ivvn的个人空间 ivvn 发布于2007-01-04 14:11:22

QUOTE:

原帖由 Rimifon 于 2007-1-4 14:04 发表
209行错误:State未定义……
加上document.getElementById可能就行了,我改一下
登陆不起啦发布于2007-01-04 16:37:50
handleStateChange其实可以不用做成静态成员.
jefflam发布于2007-01-05 16:21:07
这个真不错
我来说两句

(可选)