I'm working on a framework, and one of its most important parts is its Ajax module. Unfortunately, I am horrible with Ajax. I was just wondering if someone could help me with testing this.
Here is the code:
Code:
/* $.load(w, x, y, z) -- returns the requested file.
PARAMETERS
-----------------------------
w - url of file to load
x - the callback function. Will be passed the reponse text as a parameter
y - Boolean value representing whether the connection should be synchronous
z - an optional function that will be called if an error occurs. Will be passed the response text as a parameter
*/
var $ = {};
$.load = function(w, x, y, z) {
var httpRequest = function() {
if(window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if(window.ActiveXObject) {
var t = ["MSXML2.XmlHttp.6.0", "MSXML2.XmlHttp.3.0"];
for (var i = 0; i < 2; i++) {
try {
var n = new ActiveXObject(t[i]);
return n;
} catch(e) {}
}
}
return null;
};
httpRequest.open("GET", w, y);
if(y) {
var a1 = httpRequest;
var a2 = x;
if(z) {
var a3 = z;
} else {
var a3 = false;
}
function f() {
if(a1.readyState == 4) {
if(a1.status == 200) {
a2(a1.responseText);
} else {
if(a3 != false) {
a3(a1.responseText);
}
}
}
}
httpRequest.onreadystatechange = f;
}
httpRequest.send(null);
if(!y) {
x(httpRequest.responseText);
}
return httpRequest;
};