Javascript: Cross-Browser XMLHttpRequest Implementation
"XMLHttpRequest is an API that can be used by JavaScript, and other web browser scripting languages to transfer XML and other text data between a web page's Client-Side and Server-Side."
Internet Explorer implementation of XMLHttp is ActiveX. However Mozilla based browsers implement XMLHttp, not as an ActiveX control but as a native browser object called XMLHttpRequest.
Therefore it is a common problem when solutions utilizing XMLHttp work fine in one browser but not in the other. The following snippet is a function which creates XMLHttp object in javascript ready for use. Tested in IE and Firefox:
// Create and return XMLHttp object
function GetXmlHttp()
{
var oXmlHttp = null;
try
{
oXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(oc)
{
oXmlHttp = null;
}
}
if ((!oXmlHttp) && (typeof XMLHttpRequest != 'undefined'))
{
oXmlHttp = new XMLHttpRequest();
}
return oXmlHttp;
}
Enjoy :)
Friday, February 8, 2008 11:00 PM