469,328 Members | 1,247 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

How do I call function that a variable refers to?

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
5 40248


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
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


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
Thank you so much Martin.

Thak works :D

Sep 2 '06 #5
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.

Similar topics

5 posts views Thread by Lucy Randles | last post: by
4 posts views Thread by Dave | last post: by
5 posts views Thread by Jade | last post: by
12 posts views Thread by leaf | last post: by
7 posts views Thread by anto frank | last post: by
9 posts views Thread by Andrew Poulos | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.