The JavaScript functions (defined in the HTML head section):
Code:
function createRequestObject() {
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
//IE implements the XML request object as an ActiveX object
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
//Other browsers use a JavaScript XMLHttpRequest object
ro = new XMLHttpRequest();
}
return ro;
}
function sndReq(action,args) {
var http = createRequestObject();
http.open('get', action+'?'+Math.random(), true);
http.onreadystatechange = function(args){if(http.readyState==4){alert(args)}};
http.send(null);
}
And then the calls (right now each is calling the same simple "Hello World"-esque test page), currently sloppily tossed into the body tag's onload event (cleaned up here for ease of reading):
Code:
sndReq('test.html','1');
sndReq('test.html','2');
sndReq('test.html','3');
sndReq('test.html','4');
sndReq('test.html','5');
Right now I get a series of 5 alert boxes that each say "undefined", whereas I'm expecting to see the numbers 1 - 5 in each one (though not necessarily in order - I'm not that naive!).
The idea is to eventually use the second 'args' parameter to define where on the page the result should be displayed, e.g. via document.getElementById(args).innerHTML = XHR.responseText. However, as mentioned I can't figure out how to pass this parameter into the handler; additionally, I don't know how I'd reference an XMLHttpRequest object to retrieve the responseText without making it a global variable.
The only workable solution I've found so far is to make a bunch of global XHR objects and separate distinct code snippets to initialize and fire off each one (i.e. essentially replicate sndReq above to handle each request).
Does anyone have a solution to my problem that is cleaner than bogging down my code with a dozen different XHR objects each fired and handled separately? Essentially the core of the problem is that I have multiple requests I want to fire off simultaneously, each needs to be handled slightly differently (i.e. needs to be displayed in a different place on the screen), and I need the solution to be easily scalable in that I need to be able to easily add new targets. I do have full control over what I am requesting, so don't hesitate to offer a solution that requires that I change the way the data is sent from the server.
Bookmarks