创建XMLHttpRqruest和XMLDOM对象的一个库
我对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()
查看更多内容: js中国
下载代码
使用说明:
javascript库说明
引用:<script type="text/javascript" src="XMLHttpRequest.js"></script>
1,创建XMLHttpRqruest对象
var XMLHttp = CreateXMLHttpRequest();
2,创建XMLDOM对象
var XMLDOM = CreateXMLDOM();