There are 3 simple ways of doing this.
1: use closures as already described
2: set an attribute on the xhr object that you can reference later like so:
xhr._url = url[i];
xhr.onreadystatechange = function(readystateEvent) {
//'this' is the xhr object
console.log("Recieved data from " + this._url);
};
xhr.open("GET", "https://" + url[i], true);
3: Curry the data you need into your callbacks (my preferred solution)
Function.prototype.curry = function curry() {
var fn = this, args = Array.prototype.slice.call(arguments);
return function curryed() {
return fn.apply(this, args.concat(Array.prototype.slice.call(arguments)));
};
};
function onReadystateChange(url, readystateEvent) {
console.log("Recieved data from " + url);
};
xhr.onreadystatechange = onReadystateChange.curry(url[i]);
xhr.open("GET", "https://" + url[i], true);