Para el uso de Ajax nativamente, casi siempre uso las mismas técnicas que Jhon resig, sin embargo en ajaxian, acaban de mostrar una tecnica que segun lei es la mas rapida, ademas de ser elegante.
Con esta implementacion de Ajax, no hay que preocuparse por crear varias veces el objeto ajax,esto lo digo por que cuando empiezan con Ajax, es comĂșn caer en ese error.
Ademas se acelera el motor de ajax por la forma en que son llamadas las funciones.
Aqui les muestro el codigo
[source:javascript]
var asyncRequest = function() {
function handleReadyState(o, callback) {
if (o && o.readyState == 4 && o.status == 200) {
if (callback) {
callback(o);
}
}
}
var getXHR = function() {
var http;
try {
http = new XMLHttpRequest;
getXHR = function() {
return new XMLHttpRequest;
};
}
catch(e) {
var msxml = [
âMSXML2.XMLHTTP.3.0âČ,
âMSXML2.XMLHTTPâ,
âMicrosoft.XMLHTTPâ
];
for (var i=0, len = msxml.length; i < len; ++i) {
try {
http = new ActiveXObject(msxml[i]);
getXHR = function() {
return new ActiveXObject(msxml[i]);
};
break;
}
catch(e) {}
}
}
return http;
};
return function(method, uri, callback, postData) {
var http = getXHR();
http.open(method, uri, true);
handleReadyState(http, callback);
http.send(postData || null);
return http;
};
}();
[/source]
Para hacer el uso de esta funcion seria de esta forma
[source:javascript]
asyncRequest(’GET’, ‘foo.php’, function(o) {
alert(o.responseText);
});
[/source]
Hare unas pruebas y luego les platico como me fue con este manejo de ajax.
Visto en | Optimized Speedy Ajax Code
y en Making Ajax Elegantly Fast













