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 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Los Angeles
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Check for the existence of a function

    How do you check for the existence of a function. I know how to do it for a variable, but never tried for a function until now.

    I call a function on a page, if its not there, do nothing.

    Has any written code like this before? Can it be done in javascript? (I've googled for the last couple of hours without finding a solution.)



    ---------------------------------------------------
    I accidently posted here and couldn't delete it, if you're an mod or admin, please delete my post:
    http://www.codingforums.com/showthread.php?t=8894

  • #2
    New Coder
    Join Date
    Jul 2002
    Location
    Los Angeles
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, BTW, hoping for a solution that does not use a try catch:
    Code:
    	try {
    		funcName();
    	} catch(er) {
    		//do nothin
    	}

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Code:
    if(typeof functionname=='function')
        /* dostuff */;
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    New Coder
    Join Date
    Jul 2002
    Location
    Los Angeles
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    liorean, your code works great
    Code:
    if(typeof functionname== 'function')
        	functionname;
    However, my function has parens, () in it.
    Code:
    if(typeof functionname() == 'function')
        	functionname();
    I still get a "object expected" in IE and a "functionname is not defined" in firebird.

    I may be forced to use a try catch.

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    No, no, no. Do just as I said enter only the function name, not the parentheses. You don't want to call the function (that's what you use the parentheses for), only detect if it exists.
    Code:
    if(typeof functionname=='function')
        functionname();
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    New Coder
    Join Date
    Jul 2002
    Location
    Los Angeles
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sweet. That did the trick. I did see instructions on how to check for String, object, etc. for typeOf. I'll have to find it again to see if it mentioned the detection of functions correctly.

    Thanks again.


  •  

    Posting Permissions

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