Código para crear un objeto XMLHttpRequest


function makeAJAX() {
var xmlhttp=false;
// Entre estos ids podemos obviar comodamente
// la versión Msxml2.XMLHTTP.7.0
var ids = ["Msxml2.XMLHTTP.7.0",
"Msxml2.XMLHTTP.6.0",
"Msxml2.XMLHTTP.5.0",
"Msxml2.XMLHTTP.4.0",
"Msxml2.XMLHTTP.3.0",
"Msxml2.XMLHTTP",
"Microsoft.XMLHTTP"];
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation,
// we can cope with old IE versions.
// and security blocked creation of the objects.
for(var i=0; !xmlhttp && i < ids.length; i++) {
try {
xmlhttp = new ActiveXObject(ids[i]);
} catch(ex) {
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}
/*
* Esta condición es realmente obsoleta pues
* actualmente todos los navegadores modernos
* ya vienen con compatibilidad innata con
* ActiveXObject o con XMLHttpRequest
* también se puede eliminar del código,
* de echo yo nunca la he utilizado
*/
if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}
}
return xmlhttp;
}

Comentarios

Anónimo ha dicho que…
Muchas gracias por esta aportación, ha sido de gran utilidad!

Muchas gracias!!

Ric, Barcelona.