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

How do I call function that a variable refers to?

P: n/a
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

Sep 2 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a


yaru22 wrote:
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)

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 2 '06 #2

P: n/a
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.

Sep 2 '06 #3

P: n/a


yaru22 wrote:
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();
--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 2 '06 #4

P: n/a
Thank you so much Martin.

Thak works :D

Sep 2 '06 #5

P: n/a
yaru22 wrote:
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);}

Sep 2 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.