Forum Moderators: open

Message Too Old, No Replies

needed: AJAX loaded trigger

an event that "waits"

         

httpwebwitch

1:28 am on Nov 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a script that goes something like this:

loadXMLfromDatabase();
doSomethingWithNewElements();

The problem is my script tries to doSomethingWithNewElements() before it's finished loadXMLfromDatabase().

I get the classic Object Expected error.

I found that this works:
loadXMLfromDatabase();
alert("it's loaded now");
doSomethingWithNewElements();

which is promising, but I don't want an alert in there.

How do I wait until the XML is loaded before going on to the next command?

whoisgregg

4:49 pm on Nov 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I always fall back to the same sample code -- it's the top hit on Google for XMLHttpRequest object and, even though it wasn't there idea in the first place, Apple's done a pretty darn good job of providing sample XMLHttpRequest code [developer.apple.com].

You'll need a function like theirs, which they call from a

req.onreadystatechange
(where
req
is the reference to the XML object).

 function processReqChange() {
// only if req shows "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
// ...processing statements go here...
} else {
alert("There was a problem retrieving the XML data:\n" +
req.statusText);
}
}
}