View Full Version : how can i get the name of the function i called ?
01-16-2003, 11:12 AM
say i call function foo() how can i get the name of the function i just called that is "foo()"?
01-16-2003, 12:38 PM
Are you just wanting to call it from an expression, or do you want to test if its there?
01-16-2003, 01:01 PM
well i like to check the name of the function im calling
01-16-2003, 01: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!
var thisName = getFunctionName();
alert('The function is called: '+thisName);
rawString = getFunctionName.caller.toString();
openingBracket = rawString.indexOf("(");
prefix = rawString.toLowerCase().indexOf("function ")+9;
return rawString.substring(prefix, openingBracket);
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.