473,386 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

xmlhttprequest onreadystatechange parameters

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[i] = false;
httpRequest[i] = new XMLHttpRequest();
httpRequest[i].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[i].open('GET', URL, true);
httpRequest[i].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?

Feb 28 '07 #1
4 7964
je***********@gmail.com wrote:
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[i] = false;
httpRequest[i] = new XMLHttpRequest();
httpRequest[i].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[i].onreadystatechange = function (index) {
return function () {
if (httpRequest[index].readyState == 4) {
// continue to use use httpRequest[index] here
...
}
};
}(i);

--

Martin Honnen
http://JavaScript.FAQTs.com/
Feb 28 '07 #2
On Feb 28, 7:54 am, Martin Honnen <mahotr...@yahoo.dewrote:
jeff.maild...@gmail.com wrote:
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[i] = false;
httpRequest[i] = new XMLHttpRequest();
httpRequest[i].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[i].onreadystatechange = function (index) {
return function () {
if (httpRequest[index].readyState == 4) {
// continue to use use httpRequest[index] here
...
}
};
}(i);

--

Martin Honnen
http://JavaScript.FAQTs.com/

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

Feb 28 '07 #3
je***********@gmail.com wrote:
> httpRequest[i].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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Feb 28 '07 #4
On Feb 28, 9:44 am, Martin Honnen <mahotr...@yahoo.dewrote:
jeff.maild...@gmail.com wrote:
httpRequest[i].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.

--

Martin Honnen
http://JavaScript.FAQTs.com/


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

Feb 28 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Börni | last post by:
Dear group, i have some experience with javascript, but now i am trying to do object oriented programming with it and i am somehow stuck. I have tried the code so far only in Mozilla 1.7.3 I am...
5
by: Sean Berry | last post by:
I have an online store that a customer needs customized by having notes for each product. I have added a javascript function that sends a session id, unique product id and some user-defined notes...
42
by: Greg | last post by:
Hi, I've designed a bookmark in Ajax / PHP that I will put soon on sourceforge.net. But I've got an very tricky bug. I try it on some computers with Internet Explorer/Windows, Firefox...
6
by: Nathan | last post by:
Can I run two XMLHTTPRequest objects at the same time? Im able to get one to work without problems. If I put a call to a function inside the first ones onreadystatechange function, the 2nd ones...
1
by: mathewda | last post by:
Hey, I'm having a problem that I consider kinda weird that is alluding me at the moment. I've wrote some code that will set up an XMLHttpRequest, it then makes a call to open and send and sets the...
1
by: weston | last post by:
So, the following apparently works as a method of grafting a method onto an object (using the squarefree JS shell): obj = { x: 1, inc: null } result obj.inc = function () { this.x++ } result...
5
by: HugeBob | last post by:
Hi All, I've got a question about Asynchronous vs Synchronous mode with the XMLHttpRequest object. What are the ramifications of using one mode vs the other? If the script uses Asynchronous...
2
by: gradinafrica | last post by:
I'm trying to create a log out button that uses AJAX to call a php file which ends the current session: //logout.php <?php if (!session_start()); session_destroy(); //Destroys the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.