Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Posts
    143
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how can i get the name of the function i called ?

    hello
    say i call function foo() how can i get the name of the function i just called that is "foo()"?

  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    IN, USA
    Posts
    121
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are you just wanting to call it from an expression, or do you want to test if its there?
    Just call me Crash...
    "Crash- and BURN!"
    -Cereal Killer

  • #3
    Regular Coder
    Join Date
    Sep 2002
    Posts
    143
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well i like to check the name of the function im calling

  • #4
    New Coder
    Join Date
    Jun 2002
    Location
    London & Oxford
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!


    <script>
    <!--
    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);
    }

    //-->
    </script>

    <a href="javascript:testFunctionName()">Test</a>
    As easy as 3.1415926535897932384626433832795028841


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •