PDA

Click to See Complete Forum and Search --> : parameters and XML.onLoad



proleptic
06-15-2001, 03:44 PM
in order to make sure that a function executes when the XML is loaded, i call it like this:

myXML.onLoad = GetResponse;

however, GetResponse returns a value, and i need to capture it somehow. if it weren't for the event handler, i'd just do somthing like this:

myArray = GetReponse();

except that i need to make sure that GetResponse executes only after the XML has been loaded. is it possible to combine these two statements?

thanks,
lauren

VAYKENT
06-18-2001, 04:19 PM
You can do that really easily a couple of ways... have the onLoad handler call a function that calls the real function... the real function returns the value, which is stored or dealt with in the first calling function - did that make any sense?? Anyway, you could do that, or you could just take this value the function is supposed to return and store it outside the function.. say in... _root.nameOfVar...

There's prob'ly a million and a half ways to do what you're looking for... if that's what you're looking for...

proleptic
06-18-2001, 04:33 PM
Originally posted by VAYKENT
You can do that really easily a couple of ways... have the onLoad handler call a function that calls the real function... the real function returns the value, which is stored or dealt with in the first calling function - did that make any sense?? Anyway, you could do that, or you could just take this value the function is supposed to return and store it outside the function.. say in... _root.nameOfVar...


Because the original function will be called many times and sometimes at the same time, I don't want to use global variables, but I don't understand how I can get at that array the first way you mentioned. Even if I have the onLoad handler call the real function, the array that the real function returns will just be hanging out somewhere.

Here. THis is more detail about what I want to do:

I need to come up with a generic XML loading and processing function that, given the name of an XML file and an element to look for, returns an array of the text nodes associated with that element.

So there is an XML document called "doc.xml":


<root>
<foo>abc</foo>
<bar>def</bar>
<bar>ghi</bar>
<bar>jkl</bar>
</root>

The function I want to create you'd call from anywhere, at any time, something like:

myArray = GetXML("doc.xml", "bar");

where you'd end up with myArray: ["def", "ghi", "jkl"]

In my mind, the GetXML function would work something like this:

function GetXML (filename, query) {
myArray = new Array();
myXML = new XML();

myXML.onLoad = (myArray = ProcessXML(success, query));
myXML.load(filename);

return myArray;
}

Except problem 1: you can't pass the event handler anything other than success and problem 2: it can't return anything like that. But continuing on... (pseudo-coded for readability)

function ProcessXML(success, query) {
if (success) {
// check if a node is named whatever query is
// if so, put its associate text node into an array
// go through all the nodes
// return the array
}
}

So there are a bunch of problems here, but they all relate to the fact that I don't think you can pass any parameters in or return any values out with the
XML.onLoad handler.

How do I get around that?
[Edited by proleptic on 06-18-2001 at 04:36 PM]