Forum Moderators: open
I want to assign a dynamically created function to the "onreadystatechange" event handler of a XMLHttpRequest object. The function has a parameter which identifies the position of the HTTP request a queue. The problem is that FF throws exceptions when it reaches the eval that creates the function (highlighted below). I have tried different approches but none worked. The solution must work in FF 1+ and IE 6+.
Thank you
========== CODE START ==========
var XmlRpc = {
...
send : function(iServerUrl, iMethodName, iXmlDoc, iCallback, iAsync) {
// Increment the queue index. We use a temporary var for thread safety.
var qi = ++this.queueIndx;
if (qi > this.maxQueueIndx)
qi = this.queueIndx = 0;
// Create an XML HTTP request object.
var request = null;
if (gcIsIEXmlCompat)
request = new ActiveXObject("Microsoft.XMLHTTP");
else {
request = new XMLHttpRequest();
request.overrideMimeType('text/xml');
}
if (!request) {
this.handleError(new Error('XmlRpc::send - Creation of XML HTTP request failed'), qi);
return false;
}
// Insert the request object, the external callback and a null timeout id in the queue.
this.queue[qi] = {
request : request,
callback : iCallback,
timeout : null
};
// Send the request
request.open('POST', iServerUrl, iAsync);
request.setRequestHeader('User-Agent', gcXmlRpcUserAgent);
request.setRequestHeader('Content-Type', 'text/xml');
var self = this;
// ********** PROBLEM LINE BELOW **********
request.onreadystatechange = eval('function(){self.handleHttpResponse('+qi+')}');
// ********** PROBLEM LINE ABOVE **********
request.send(iXmlDoc);
// If the request doesn't complete within N seconds, abort!
if (iAsync) {
var id = eval('window.setTimeout(function(){XmlRpc.abort('+qi+')}, gcXmlRpcTimeout)');
this.queue[qi].timeout = id;
}
return true;
},
...
};
========== CODE END ==========
qi should be available. request.onreadystatechange = function(){self.handleHttpResponse(qi)}; Here's an isomorphism (er..I think).
var obj =
{output: function(inp)
{
alert(inp);
},doo: function()
{
var arg = 6;
var self = this;
this.innerDoo = function(){ self.output(arg)};
}
}obj.doo()
obj.innerDoo()
What I need is to be able to generate a function that contains a call to a function which has a static parameter, e.g.
function(){self.handleHttpResponse(1)}, function(){self.handleHttpResponse(2)}, function(){self.handleHttpResponse(3)}, etc. The parameter is the position of the XML HTTP request in a queue of requests.
When FF tries to compile
request.onreadystatechange = eval('function(){self.handleHttpResponse('+qi+')}');
I get the following message in the Javascript Console:
Error: syntax error
Source file: ...
funtion(){self.handleHttpResponse(1)}
Thank you