Topic: Running a function by name? |
|
---|---|
Author | Thread |
Bipolar (III) Inmate From: Australia |
posted 04-09-2006 18:20
I'm sure i've done this before but for the life of me i can't find any reference to it. code: function f1(f2Name) { f2Name(); } f1('f2') function f2() { alert('blah') }
|
Bipolar (III) Inmate From: Australia |
posted 04-09-2006 18:30
Argg... nevermind - was putting brackets in wrong places... think i should go sleep some |
Paranoid (IV) Inmate From: Norway |
posted 04-09-2006 18:39
How you wrote your f1() function implies that you pass the handle of a function, hence you should do: code: f1( f2 ); But for what you want, your f1() function should look like: code: function f1( functionName ) { var theFunction=window[functionName]; if( typeof(theFunction)=='function') return theFunction(); // fallback return null; } |
Bipolar (III) Inmate From: Australia |
posted 04-09-2006 18:46
Cheers, it does. What i did above would have worked if i left the quotes off f1('f2') - should be f1(f2) - then its the same as what you did, without the step declaring the function variable. |