Rik wrote:
bobzimuta wrote:
<snip>
if (window.ActiveXObject && !window.XMLHttpRequest) {
window.XMLHttpRequest = function() {
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) {
}
You realise that if you do that when, say, Msxml2.XMLHTTP is the
version used your code will try three alternatives which will error
prior to returning the object, and it will do that on each and every
occasion the replacement - XMLHttpRequest - constructor is used.
}
return false;
Returning false form a function used as the operand of the - new -
operator does not result in boolean false being returned, instead the
Native ECMAScript object that is being constructed is returned here.
};
}
Which would mean in the rest of the code you can just use:
request = new XMLHTTPRequest;
if(!request){
//errorhandling, fallbacks etc.
Because when IE cannot create an XML HTTP request object (for example
when ActiveX is disabled) your - new XMLHTTPRequest - expression
evaluated as a Native ECMAScript object, and such an object
type-converts to true in and - if - expression, this "errorhandling,
fallbacks etc." code will _never_ be executed, and instead the code
will take the other branch and treat the Native ECMAScript object as an
XML HTTP request object, erroring out as soon as it tries to call a
method of the object.
} else {
//go on
}
function XMLHttpRequestFactory(){
var msxmls, comString, xmlObj = null, index;
if(
(window.XMLHttpRequest)&&
(xmlObj = (new XMLHttpRequest()))
){
/* Replace the factory with an version uses the available -
XMLHttpRequest - constructor:-
*/
XMLHttpRequestFactory = function(){
return new XMLHttpRequest();
};
}else if(window.ActiveXObject){
msxmls = [
'Msxml2.XMLHTTP.5.0',
'Msxml2.XMLHTTP.4.0',
'Msxml2.XMLHTTP.3.0',
'Msxml2.XMLHTTP',
'Microsoft.XMLHTTP'
];
for(index = 0;index < msxmls.length;++index){
try{
if((xmlObj = new ActiveXObject(msxmls[index]))){
comString = msxmls[index];
/* Replace the factory with an version that directly used
the successful string, as determined by its being the
string that resulted in the creation of the XML HTTP
request object without any exceptions being thrown:-
*/
XMLHttpRequestFactory = function(){
return new ActiveXObject(comString);
};
msxmls = null;
break;
}
}catch(e){;}
}
if(!xmlObj){
/* Replace the factory with a dummy that just returns null as
there is no mechanism for creating the XML HTTP request
objects in this environment:-
*/
XMLHttpRequestFactory = function(){
return null;
};
}
}else{
/* Replace the factory with a dummy that just returns null as there
is no mechanism for creating the XML HTTP request objects in
this environment:-
*/
XMLHttpRequestFactory = function(){
return null;
};
}
return (function freeRefs(arg){
xmlObj = null; //guarantee that any ActiveX object is not stored in
//this closure.
return arg;
})(xmlObj);
}
Richard.