Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts

    Calling a js function when name is stored in a variable

    I have a function name stored in a variable - how do I call the function?

  2. Users who have thanked NancyJ for this post:

    abduraooft (06-18-2008)

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,013
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Try:-

    var test = "functionName";
    eval(test + '()');

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Code:
    objectToCallWith[string](params);
    If the function is a global function, use window.

    Code:
    function succ(x) {
       return x + 1;
    }
    
    var str = "succ";
    
    var a = succ(1);
    var b = window["succ"](2);
    var c = window[str](3);
    
    var f = window["succ"];
    var d = f(4);
    Trinithis

  • Users who have thanked Trinithis for this post:

    Forever Zero (06-18-2008)

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Is it a global function? If so, then you can do window[name]();. If it's a local function, you need to use eval(name)(); or eval(name+'()'); like Philip M suggests.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • Users who have thanked liorean for this post:

    NancyJ (06-17-2008)

  • #5
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    um... whats the difference between a local and global function?

  • #6
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Edit: A local function is a function defined within another function.

    I have no idea if this is a bad idea or not (I just thought of it), but for local functions, you can avoid eval for them if you attach them to a variable that denotes the local scope:

    Code:
    function f() {
      var g = function() {
        alert("g");
      };
      var local = {
        g: g
      };
      var str = "g";
      local[str]();
    }
    f();
    Trinithis

  • Users who have thanked Trinithis for this post:

    NancyJ (06-17-2008)

  • #7
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    huh. Didn't know you could define functions within functions. I guess mine would be a global function then heh.

  • #8
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,854
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Quote Originally Posted by NancyJ View Post
    I have a function name stored in a variable - how do I call the function?
    Subscribed and saved this thread for reference
    Good question and many thanks
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,013
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Thanks all round - except for the guy who provide the answer!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •