Actually IE8 does have a constructor for XMLHttpRequest. Either way, your javascript code to create the XHR object (wether it's by XMLHttpRequest or ActiveX) is weird. You have an empty try block, which means that if it enters that try block, no code will be run, and the catch block following this will obviously never be executed. If you indendted your code properly you'd see this straight away. Once you're done debugging, you can get rid of whitespaces if you wish. Moreover, if you ever want anyone else to read your code, do provide indented code.
if("undefined"!=typeof XMLHttpRequest)
{
xajax.tools.getRequestObject=function()
{
return new XMLHttpRequest();
}
}
else if("undefined"!=typeof ActiveXObject)
{
xajax.tools.getRequestObject=function()
{
try
{
return new ActiveXObject("Msxml2.XMLHTTP.4.0");
}
catch(e)
{
try
{
return new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e2)
{
// empty try block...
try
{
// nothing happens, but no exception will ever be thrown
}
// unreachable code
catch(e3)
{
return new ActiveXObject("Microsoft.XMLHTTP");
}
}
}
}
}
else if(window.createRequest)
{
xajax.tools.getRequestObject=function()
{
return window.createRequest();
}
}
else
{
xajax.tools.getRequestObject=function()
{
return null;
}
}
But I still agree with Weedpacket. Use try blocks to try executing code, and use catch blocks to recover if what you want to do is not possible, rather than use an if check to see if something is not undefined and then assume that you know what it is (other than undefined).
For example, what you do could actually be equivalent to
var x = false;
if (x != undefined)
{
// This code executes, since x isn't undefined, it's false
try
{
return new x();
}
catch (e)
{
// But just because x is not undefined doesn't mean it's an object with a constructor
// and since it's false rather than anything else... you get
// TypeError: x is not a constructor
alert(e);
}
}
A better way to define createRequestObject is
function createRequestObject()
{
try
{
return new XMLHttpRequest();
}
/* that code throws an exception if XMLHttpRequest doesn't have a constructor
* which means all browsers that doesn't give an XMLHttpRequestObject will try this
*/
catch(e)
{
try
{
return new ActiveXObject("Msxml2.XMLHTTP.4.0");
}
catch(e)
{
try
{
return new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
// empty try block...
try
{
return new ActiveXObject("Microsoft.XMLHTTP");
}
// unreachable code
catch(e)
{
try
{
return window.createRequest();
}
catch(e)
{
return null;
}
}
}
}
}
}
And on a side note, the only browser I've ever read about that supports window.createRequest is IceBrowser whose life cycle has come to an end
http://www.icesoft.com/products/icebrowser.html wrote:
End Of Service Life: December 31, 2010
… no further "hot fixes" will be available.
… all product support ceases.
… the product is no longer sold, maintained, or supported.
The two errors I get in IE8 are
Can't execute code from a freed script - see this page of possible solutions.
Object doesn't support the property or method which indicates this line (broken up into several lines and properly indented
if(1 < numArgs)
oRequest=arguments[1];
oRequest.functionName=arguments[0];
var xx=xajax;
xx.initializeRequest(oRequest);
xx.processParameters(oRequest);
while (0 < oRequest.retry)
{
try
{
--oRequest.retry;
xx.prepareRequest(oRequest);
return xx.submitRequest(oRequest);
}
catch(e)
{
xajax.callback.execute(
Since IE8 doesn't seem to actually indicate a property or method - it indicates a closing }, even though the error message is "doesn't support property or method", I'd start tracking variables in IE8 and see what they are, and also make sure wether it's a method in the try block that fails or in the catch block. I believe you should be able to set variables to watch in IE8's debugger, else you can use things like
alert('oRequest: ' + oRequest + "\n" + 'xx: ' + xx + "\n" + xx.prepareRequest + "and so on...");
Or, if you wish to list all properties of an object
var s = '';
// show all properties of oRequest
for (x in oRequest)
{
s += 'oRequest['+ x + ']: ' + oRequest[x] + "\n";
}
s += "\n";
for (x in xx)
{
s += 'xx['+ x + ']: ' + xx[x] + "\n";
}
alert(s);
... which of course may be too long to fit in an alert. But you could always add the debug output to your current document
document.appendChild(document.createTextNode(s));