By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,255 Members | 2,640 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,255 IT Pros & Developers. It's quick & easy.

Returning responseXML, set within an anonymous function, only once readyState = 4

P: n/a
I have several functions with code along the lines of:

var xmlDoc = requestXML("ajax.asp?SP=SelectRelatedTags&tag=" +
array[i]);

The requestXML() function includes the code:

var xmlDoc = null;
http_request.onreadystatechange = function() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
xmlDoc = http_request.responseXML;
} else {
alert('There was a problem with the request.' +
http_request.status);
}
}
};
http_request.open('GET', url, true);
http_request.send(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.

Thanks,
Iain

Dec 4 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
iporter wrote:
I have several functions with code along the lines of:

var xmlDoc = requestXML("ajax.asp?SP=SelectRelatedTags&tag=" +
array[i]);

The requestXML() function includes the code:

var xmlDoc = null;
http_request.onreadystatechange = function() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
xmlDoc = http_request.responseXML;
} else {
alert('There was a problem with the request.' +
http_request.status);
}
}
};
http_request.open('GET', url, true);
http_request.send(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.onreadystatechange 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.onreadystatechange = function() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
xmlDoc = http_request.responseXML;
processData(xmlDoc);
} else {
alert('There was a problem with the request.' +
http_request.status);
}
}
}
Notice I added the line "processData(xmlDoc)". 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
Dec 4 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.