我對AJax的理解:
在web客戶端,XMLHttpRequest和服務器交換數據(get,post),通過ResponseText或者ResponseXML獲取數據後,將數據轉交XMLDOM對象處理,最終交由HtmlDOM對象輸出到頁面上。
所以,要精通AJax,其實是要精通三個對象:XMLHttpRequest,XMLDOM,HtmlDOM.
重所周知,ie和非IE下創建這兩個對象的方法不同,為了統一創建方式,我將他們做了下簡單封裝。
/*
/返回XMLHttpRequest對象
/
*/
function CreateXMLHttpRequest()
{
var req;
_XMLHTTP = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
_newActiveXObject = function(axarray)
{
var returnValue;
for (var i = 0; i < axarray.length; i++)
{
try
{returnValue = new ActiveXObject(axarray[i]);
break;
}
catch (ex) {}
}
return returnValue;
};
if (window.XMLHttpRequest)
{
req = new XMLHttpRequest();
}
else if (window.ActiveXObject && !(navigator.userAgent.indexOf("Mac") >= 0 && navigator.userAgent.indexOf("MSIE") >= 0))
{
req = _newActiveXObject(_XMLHTTP);
}
return req;
}
//End CreateXMLHttpRequest()
//--------------------------------------------------------------------------------------------------------------------------//
/*
/返回XMLDOM對象
/
*/
function CreateXMLDOM()
{
var req;
_DOMDocument = ["MsXML2.DOMDocument.6.0", "MsXML2.DOMDocument.5.0", "MsXML2.DOMDocument.4.0", "MsXML2.DOMDocument.3.0", "MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XMLDOM"];
_newActiveXObject = function(axarray)
{
var returnValue;
for (var i = 0; i < axarray.length; i++)
{
try
{returnValue = new ActiveXObject(axarray[i]);
break;
}
catch (ex) {}
}
return returnValue;
};
if (window.XMLHttpRequest)
{
req = document.implementation.createDocument("", "", null);
}
else if (window.ActiveXObject && !(navigator.userAgent.indexOf("Mac") >= 0 && navigator.userAgent.indexOf("MSIE") >= 0))
{
req = _newActiveXObject(_DOMDocument);
}
return req;
}
//End CreateXMLDOM()