na*****@gmail.com wrote:
Is there a way to "pass" an XMLHttpRequest object to the callback
function for onreadystatechange? Or a way to access it from
onreadystatechange? I would like to avoid the use of a global
variable...
xmlHttp = new XMLHttpRequest();
function myFunc()
{
...
xmlHttp.onreadystatechange = myCallBack;
You can use a local variable declared and initialized in the function e.g.
var xmlHttp = new XMLHttpRequest();
and then use a function expression exploiting closures e.g.
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
...
}
};
That would require to use the stuff you have in the callback function
into the body of the function expression.
Or if you want to keep the global callback function then you could use a
function expression with a simple body calling your callback and passing
in the request object e.g.
xmlHttp.onreadystatechange = function () {
myCallBack(xmlHttp);
};
and then of course you need a parameter for your global callback
function e.g.
function myCallBack (httpRequest) {
if (httpRequest.readyState == 4) {
..
}
}
--
Martin Honnen
http://JavaScript.FAQTs.com/