I have the name of a function in a variable - how can I call this function? In PHP I'd use call_user_func($funcName); PHP: but I don't know enough javascript.. any help is much appreciated. Thanks!
<script type="text/javascript"> function foo(){ alert('bar'); } var f='foo'; eval(f+'()'); </script> HTML:
The correct method is function_name.call(arguments here) Check http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Function:call
Thanks but that way doesn't work: Error: func.call is not a function Where func is the variable holding the function name. eval(func+'()') works fine though.
func should not be a variable. It should be the function name itself function foo(var1, var2, var3){ } foo.call(thisObj, var1, var2, var3) Code (markup): The first variable is the object to be used as the current object, if you are using OOP.