xmlhttprequest onreadystatechange parameters

J

jeff.maildump

This is my first firefox extension, and I'm trying to collect some
information from several sites and mash'em up into one page. But I
need to do processing specific to each asynchronous request. All the
examples I've found use a global httpRequest object that is processed
by a callback function. But I can never know what request is actually
in the httpRequest at a given time. So I tried to make an array of
such requests. But I can't seem to get around the global problem
because I can't seem to tell the processing functions what they should
process.

I thought this would work, but apparently because of closures or
something, the i variable is global with respect to the anonymous
function:

//----------------- start code -----------------------
var httpRequest = Array();
// urls is an array of URLs
// searchData is an array the same size as URLs containing data to be
searched on the corresponding URL

for (var i=0; i< urls.length; i++) {
httpRequest = false;
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
var id = i;
alert("der "+ id + " " + httpRequest[id]);
if (httpRequest[id].readyState == 4) {
if (httpRequest[id].status == 200) {
var processedData = getData(httpRequest[id].responseText,
searchData[id]);
}
}
};
httpRequest.open('GET', URL, true);
httpRequest.send(null);
}
//------------------ end code -----------------------

Any ideas on how I can make my callback function (anonymous here, but
didn't work the normal way either) know which httpRequest it is
answering?
 
M

Martin Honnen

var httpRequest = Array();
// urls is an array of URLs
// searchData is an array the same size as URLs containing data to be
searched on the corresponding URL

for (var i=0; i< urls.length; i++) {
httpRequest = false;
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
var id = i;
alert("der "+ id + " " + httpRequest[id]);
if (httpRequest[id].readyState == 4) {
if (httpRequest[id].status == 200) {
var processedData = getData(httpRequest[id].responseText,
searchData[id]);
}
}
};

httpRequest.onreadystatechange = function (index) {
return function () {
if (httpRequest[index].readyState == 4) {
// continue to use use httpRequest[index] here
...
}
};
}(i);
 
J

jeff.maildump

var httpRequest = Array();
// urls is an array of URLs
// searchData is an array the same size as URLs containing data to be
searched on the corresponding URL
for (var i=0; i< urls.length; i++) {
httpRequest = false;
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
var id = i;
alert("der "+ id + " " + httpRequest[id]);
if (httpRequest[id].readyState == 4) {
if (httpRequest[id].status == 200) {
var processedData = getData(httpRequest[id].responseText,
searchData[id]);
}
}
};


httpRequest.onreadystatechange = function (index) {
return function () {
if (httpRequest[index].readyState == 4) {
// continue to use use httpRequest[index] here
...
}
};
}(i);





Hi, thanks for the quick reply. Where is index initially set? When
does the first function get passed this index?
 
M

Martin Honnen

httpRequest.onreadystatechange = function (index) {
return function () {
if (httpRequest[index].readyState == 4) {
// continue to use use httpRequest[index] here
...
}
};
}(i);
^^^
^^^

Where is index initially set? When
does the first function get passed this index?


The loop variable i is passed in as the argument to the anonymous function.
 
J

jeff.maildump

httpRequest.onreadystatechange = function (index) {
return function () {
if (httpRequest[index].readyState == 4) {
// continue to use use httpRequest[index] here
...
}
};
}(i);

^^^
^^^

Where is index initially set? When
does the first function get passed this index?


The loop variable i is passed in as the argument to the anonymous function.




Oh yes, I overlooked the i at the very end. I'll try that when I get
home tonight. Thanks a lot!.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top