Dynamically call a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ataxia1
    New Member
    • Feb 2009
    • 5

    Dynamically call a function

    For the project I'm working on, I need to dynamically call one of several functions (which are dynamically named by asp.net). How do I call a function when I have the function name in a string? A case statement is not an option, because the function names are dynamic.
    Code:
    function function1() {
       ...
    }
    function function2() {
        ...
    }
    var functionName = 'function1'
    ...what next?

    How do I execute the function whose name is stored in functionName?

    Thanks
    Last edited by gits; Sep 25 '09, 02:56 AM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    when the functions are declared in the window-scope then you might use:

    Code:
    window['yourFunctionName']();
    to call the function.

    kind regards

    Comment

    • ataxia1
      New Member
      • Feb 2009
      • 5

      #3
      Thanks. I figured there was a pretty easy way to do it.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        no problem ... in case you would have more questions just post back to the forum ...

        kind regards

        Comment

        Working...