Having some trouble with a piece of code meant to run smoothly on Firefox 3.6. The issue arises when the variable this.xmlhttp, defined in STEP2 and used in STEP3, seems to be operating in different variable environments. Even though I expect the two usages in server_request and callback_function to refer to the same member object in the query_request_manager superobject that is defined below. Interestingly, a similar piece of code without asynchronous callbacks functions as intended during server response.
function Generic_server_request(server_location, server_file, client_callback_function)
{
this.server_location = server_location;
this.server_file = server_file;
this.query_parameters = "";
this.client_callback_function = client_callback_function;
this.xmlhttp = undefined;
} // STEP1 initializes xmlhttp as undefined
Generic_server_request.prototype.callback_function = function ()
{
if (this.xmlhttp.readyState === 4 // STEP3 ERROR xmlhttp is undefined
// I expected it to reference the object defined at STEP2
// but according to Firebug, it's not working as anticipated
// Conversely, similar code without asynchronous callbacks
// operates as expected: no issues with undefined variables
&& this.xmlhttp.status === 200)
{
this.client_callback_function(
this.xmlhttp.responseText);
}
else if (this.xmlhttp.status !== 200 || (this.xmlhttp.status === 200 && this.xmlhttp.readyState !== 2 && this.xmlhttp.readyState !== 3))
{
alert("readystate " + this.xmlhttp.readyState + " status " + this.xmlhttp.status);
}
};
Generic_server_request.prototype.server_request = function ()
{
this.xmlhttp = new XMLHttpRequest(); // STEP2 defines xmlhttp for use
this.xmlhttp.onreadystatechange = this.callback_function; // server callback to prototype.callback
this.xmlhttp.open("GET", this.server_location + this.server_file + this.query_parameters, true);
this.xmlhttp.send();
};
Generic_server_request.prototype.set_query_parameters = function (query_parameters)
{
this.query_parameters = query_parameters;
};
var query_request_manager;
function do_querry()
{
server_querry("test");
}
function server_querry(input)
{
if (query_request_manager === undefined)
{
query_request_manager = new Generic_server_request( // defining the object
"http://localhost/cgi-bin/", "querry_handler.php", status_text);
}
query_request_manager.set_query_parameters("?input=" + input);
query_request_manager.server_request();
} // utilizing the object
// end of javascript
<input type="button" value="Enter" onclick="do_querry();" />