iporter wrote:
I have several functions with code along the lines of:
var xmlDoc = requestXML("aja x.asp?SP=Select RelatedTags&tag =" +
array[i]);
The requestXML() function includes the code:
var xmlDoc = null;
http_request.on readystatechang e = function() {
if (http_request.r eadyState == 4) {
if (http_request.s tatus == 200) {
xmlDoc = http_request.re sponseXML;
} else {
alert('There was a problem with the request.' +
http_request.st atus);
}
}
};
http_request.op en('GET', url, true);
http_request.se nd(null);
return xmlDoc;
However, the last line (the return) executes before the readyState
reaches 4. How do I return the xmlDoc to the functions only once the
xmlDoc has been assigned? I tried putting the return statement in a
while loop with the condition that the readyState must = 4 - this
worked, but makes the browser popup a message saying the script is
slowing down the system.
Once you start http_request, all the communication between the browser
and server happens in the background. This is the reason return xmlDoc
isn't passing anything back.
http_request.on readystatechang e defines a function that is called by
http_request when the ready state changes. So what you need to do is
to make that line read...
http_request.on readystatechang e = function() {
if (http_request.r eadyState == 4) {
if (http_request.s tatus == 200) {
xmlDoc = http_request.re sponseXML;
processData(xml Doc);
} else {
alert('There was a problem with the request.' +
http_request.st atus);
}
}
}
Notice I added the line "processData(xm lDoc)". This will call a
function you make (named processData) which will receive the response,
allowing you to process the return data.
If you TRULY want the browser to basically do nothing until it hears
back from the server then you need to scrap all this code and research
synchronous javascript and xml :)
---------------------------------------------------------------------------
http://www.hunlock.com -- Permanently under construction (And proud of it!)
$FA