SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Alert function name

    Greetings, I would like to be able to alert the name of the current function, is there a way to do that? In the exmple below, I want to alert a string "testme". Thanks.

    Code:
    function testme()
    {
       alert(this.name);	       //This doesn't work. Is there something similar to this?
    }

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a way to do it sorta:
    Code:
    function testme()
    {
    	alertthing();
    }
    function alertthing() {
    	var callerName = alertthing.caller.toString();
    	var cn = callerName.match( /^function\s*(\s+[\w]+)\([^\(\)]*\)/ );
    	cn = cn[1] ? cn[1] : "don't know";	
    	alert(cn);
    }

  3. #3
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why would you want to do this?

    Same idea as the previous post. Perhaps more accessible for us reg exp dummies...

    alert(arguments.callee.toString().split('{')[0].split(' ')[1])

    Brett

  4. #4
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey guys, thanks for this tip. I'm working on putting together a function to handle JS errors. Is there a way to return the URL or the file name (JS file name) of the caller function. Thanks.


Bookmarks

Posting Permissions

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