出處:http://www.smallrain.net/study_show.asp?id=703
<div id="Error"></div>
<div id="State"></div>
<div id="DownloadEnd"></div>
<Script Language="JavaScript">
<!--
// more javascript from http://www.smallrain.net
function Ajax(OnError,OnState,OnDownloadEnd)
{
?// 錯誤字符串
?this.ErrorStr ??= null;
?// 錯誤事件驅動,當發生錯誤時觸發
?this.OnError ??= OnError;
?// 狀態事件驅動,當狀態改變時觸發
?this.OnState ??= OnState;
?// 完成事件驅動,當類操作完成時觸發
?this.OnDownloadEnd ?= OnDownloadEnd;
?// XMLHTTP 發送數據類型 GET 或 POST
?this.method??= "GET";
?// 將要獲取的URL地址
?this.URL??= null;
?// 指定同步或異步讀取方式(true 為異步,false 為同步)
?this.sync??= true;
?// 當method 為 POST 時 所要發送的數據
?this.PostData??= null
?// 返回讀取完成后的數據
?this.RetData ??= null;
?// 創建XMLHTTP對像
?this.HttpObj ??= this.createXMLHttpRequest();
?if(this.HttpObj == null)
?{
??// 對像創建失敗時中止運行
??return;
?}
?var Obj = this;
?// 調用事件檢測
?this.HttpObj.onreadystatechange = function()
?{
??Ajax.handleStateChange(Obj);
?}
}
// UTF 轉入 GB (by:Rimifon)
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);
}
// 創建XMLHTTP對像
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;
}
// 發送HTTP請求
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)
{
?document.getElementById("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;
?}
?document.getElementById("State").innerHTML = strState;
}
// 完成回調事件函數
function EventDownloadEnd(strValue)
{
?document.getElementById("DownloadEnd").innerHTML = strValue;
}
// 初始化Ajax對像,引入事件回調函數
var A1 = new Ajax(EventError,EventState,EventDownloadEnd);
// 指定method數據發送類型
A1.method = "GET";
// 指定URL地址
A1.URL = "// 指定為異步處理
A1.sync = true;
//發送請求
A1.send();
//-->
</Script>?