View Full Version : how can i get the name of the function i called ?

01-16-2003, 12:12 PM
say i call function foo() how can i get the name of the function i just called that is "foo()"?

01-16-2003, 01:38 PM
Are you just wanting to call it from an expression, or do you want to test if its there?

01-16-2003, 02:01 PM
well i like to check the name of the function im calling

01-16-2003, 02:25 PM
I don't know if it's possible to get the function name directly. I'd be interested if anyone knows a way.

What I have done is to 'cheat' by creating a new function which looks at the caller property of the function which calls it. This string is then chopped up so it only consists of the name and this is then passed back to the original (calling) function. See the example below.

There must be a better way of doing this!

function testFunctionName(){
var thisName = getFunctionName();
alert('The function is called: '+thisName);

function getFunctionName(){
rawString = getFunctionName.caller.toString();
openingBracket = rawString.indexOf("(");
prefix = rawString.toLowerCase().indexOf("function ")+9;
return rawString.substring(prefix, openingBracket);


<a href="javascript:testFunctionName()">Test</a>