How do I call function that a variable refers to?

Discussion in 'Javascript' started by yaru22, Sep 2, 2006.

  1. yaru22

    yaru22 Guest

    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
    yaru22, Sep 2, 2006
    #1
    1. Advertising

  2. 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/
    Martin Honnen, Sep 2, 2006
    #2
    1. Advertising

  3. yaru22

    yaru22 Guest

    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.
    yaru22, Sep 2, 2006
    #3
  4. 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/
    Martin Honnen, Sep 2, 2006
    #4
  5. yaru22

    yaru22 Guest

    Thank you so much Martin.

    Thak works :D
    yaru22, Sep 2, 2006
    #5
  6. yaru22

    Davey Erwin Guest

    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);}
    Davey Erwin, Sep 2, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Behzad
    Replies:
    6
    Views:
    699
    Juan T. Llibre
    Apr 27, 2007
  2. sar
    Replies:
    1
    Views:
    223
  3. ashish_gokhale
    Replies:
    0
    Views:
    287
    ashish_gokhale
    May 5, 2005
  4. Henry Law
    Replies:
    4
    Views:
    131
    Tad McClellan
    Jun 24, 2005
  5. Dieter Maurer
    Replies:
    0
    Views:
    224
    Dieter Maurer
    Jun 15, 2012
Loading...

Share This Page