CODE
function ajax(param) {
var xmlHttp;
var retval = null;
try {
xmlHttp = new XMLHttpRequest();
} catch (e) {
alert("Browser does not support AJAX.");
return;
}
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState==4) {
retval = xmlHttp.responseText;
}
};
xmlHttp.open("GET",("response3.php?action="+param),true);
xmlHttp.send(null);
return retval;
}
var xmlHttp;
var retval = null;
try {
xmlHttp = new XMLHttpRequest();
} catch (e) {
alert("Browser does not support AJAX.");
return;
}
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState==4) {
retval = xmlHttp.responseText;
}
};
xmlHttp.open("GET",("response3.php?action="+param),true);
xmlHttp.send(null);
return retval;
}
However, retval is out of scope in the function pointer. In other words, the line of code " retval = xmlHttp.responseText; " is creating a new variable called retval in a new scope that has absolutely nothing to do with the "retval" declared at the beginning of the ajax() function. So my question is, how do I refer to the retval in the super scope? I want to make my code reusable instead of having to define unique ajax() functions that display their responseText by changing the innerHTML of different HTML elements.
If you have a better way of AJAX-ing than my code, please suggest it because I really hate using function pointers in this case
Thanks.
