View Full Version : Treat string as a function

10-17-2006, 10:10 PM
Is there anyway to treat a string as a function name? To make something like this work(it doesn't)

somestring = 'function_name';
window.onload = somestring
function function_name(){

10-18-2006, 03:53 AM
I think JavaScript doesn't allow any such feature like PHP (http://www.php.net/manual/en/functions.variable-functions.php)

10-18-2006, 06:47 PM
Not quite. However, when you define a global function or variable, it becomes a property of the global object. From here, you can access it by its name using the ECMAScript objects-as-associative-arrays idea:
var somestring = 'function_name';
window.onload = window[somestring];
function function_name(){}

10-18-2006, 09:19 PM
Thanks Twey! Exactly what I was looking for.

10-19-2006, 03:59 AM
hmmm thats a new thing for me thanks for the info Twey