How do I call function that a variable refers to?

Y

yaru22

Hi. I'm wondering if there is a way to call function that a variable
refers to.

I was writing a script to load xml file.

And what I've written so far looks like this:

var xmlDoc;

function loadXML(xmlFile,xmlMethod)
{
if (document.implementation &&
document.implementation.createDocument) {
xmlDoc = document.implementation.createDocument("","",null);
xmlDoc.load(xmlFile);
xmlDoc.onload = ???????????;
} else if (window.ActiveXObject) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load(xmlFile);
????????????
} else {
alert("Your browser cannot handle the script.");
return;
}
}

I don't know how to process at '????????' part.

What I wanted to do was calling a function that xmlMethod variable
refers to.

Is there such a way to do it?

Thank you.

Regards,
Brian
 
M

Martin Honnen

yaru22 said:
I'm wondering if there is a way to call function that a variable
refers to.

The same way you call any function e.g.
varName()
or e.g.
varName(arg1, arg2)
 
Y

yaru22

I tried as Martin said and that doesn't seem to work.

If what Martin said was true, then the following piece of code should
print "hello world":

var test="hi";

test();

function hi()
{
document.write("hello world");
}

================
However, when I tried it, it didn't work.

Does anyone know how to call a function that the value of a variable
refers to?

In the above example, since the value of the variable test is hi, I'd
like to call the function named hi().

Thank you.
 
M

Martin Honnen

yaru22 said:
I tried as Martin said and that doesn't seem to work.

If what Martin said was true, then the following piece of code should
print "hello world":

var test="hi";

test();

function hi()
{
document.write("hello world");
}

Pass a function and _not_ a string e.g.
var test = hi;
test();
 
D

Davey Erwin

yaru22 said:
var test="hi";

test();

function hi()
{
document.write("hello world");
}

To call a function with
it's string name ...

var test="hi";
window[test]();
function hi(){alert(test);}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top