check if function name which is stored in a variable exist

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • andho
    New Member
    • Sep 2007
    • 34

    check if function name which is stored in a variable exist

    for example in this code:
    [CODE=javascript]var funcName = "myFunc";

    if (typeof eval(funcName) == 'function') {
    evail(funcName + "()"); // this will call myFunc
    }[/CODE]
    but its returns runtime error: myFunc does not exist

    what im trying to do is call the function specified in the funcName variable (which is myFunc).
    I can call is using
    [CODE=javascript]eval(funcName)[/CODE]
    but i want to check if it exists before i call it
    Last edited by pbmods; Sep 25 '07, 12:00 AM. Reason: Changed [CODE] to [CODE=javascript].
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    welcome to TSDN ...

    why do you use eval? ... in case you don't need it ... and i think so ... you may do something like the following:

    [CODE=javascript]function myFunc() {
    alert('test');
    }

    var funcName = myFunc;

    if (typeof funcName != 'undefined') {
    funcName();
    }
    [/CODE]
    kind regards

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, andho. Welcome to TSDN!

      Have a look at this article.

      Comment

      • andho
        New Member
        • Sep 2007
        • 34

        #4
        thanks for the welcome and quick replies. checked that article. but heres my real problem.
        i have this HTML:
        [HTML]<input type="text" name="single" id="single" onkeydown="vali date(event, 'number');" />[/HTML]
        On the onkeydown event i call validate function which has two parameters. One is the event and the other is the type of validation to execute, in this case 'number'. It could be varchar or date depending on the how the user wants to use it.
        now i need to check if 'number' is a function and then call it:
        Code:
        function validate(e, type) {
        	eval(type + "();");
        }
        im using eval because the type string could be any validation function in this library

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Ok!
          Have a look at this modified Code.

          [code=javascript]
          function validate(e, type) {
          var command_str = "if(typeof " + type + " == 'function') " + type + "();";
          eval(command_st r);
          }
          [/code]

          Good Luck with your try. :-)

          Kind regards,
          Dmjpro.

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            hi ...

            have a look at the following example that avoids the eval (test it in FF):

            [CODE=javascript]
            <script type="text/javascript">
            function number(e) {
            alert(e.target. nodeName);
            }

            function validate(e, func) {
            if (typeof window[func] != 'undefined') {
            window[func](e);
            }
            }
            </script>

            <input type="button" value="test_but ton" onclick="valida te(event, 'number');"/>
            [/CODE]

            kind regards

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              Originally posted by gits
              hi ...

              have a look at the following example that avoids the eval (test it in FF):

              [CODE=javascript]
              <script type="text/javascript">
              function number(e) {
              alert(e.target. nodeName);
              }

              function validate(e, func) {
              if (typeof window[func] != 'undefined') {
              window[func](e);
              }
              }
              </script>

              <input type="button" value="test_but ton" onclick="valida te(event, 'number');"/>
              [/CODE]

              kind regards
              hi Gits :-)
              Does not eval work in FF.

              Kind regards,
              Dmjpro.

              Comment

              • andho
                New Member
                • Sep 2007
                • 34

                #8
                hey it works like a charm, but really the if statement is a little longer but i go with that. But is there a better way if i have a very long if statement

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5390

                  #9
                  of course it works ... but have a look at it and replace the 'a' in it with an 'i' ... never use eval when there is no need for it ... it evaluates everything and it may lead to the phenomenon to use it for everything ... but in nearly all cases you simply don't need it ...

                  kind regards

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5390

                    #10
                    and of course you could use:

                    [CODE=javascript]
                    function validate(e, func) {
                    if (typeof window[func] == 'function') {
                    window[func](e);
                    }
                    }
                    [/CODE]

                    Comment

                    • andho
                      New Member
                      • Sep 2007
                      • 34

                      #11
                      hey window[func] is nice. havent seen it anywhere before
                      how is the compatibility of window[func] with all the browsers.

                      Comment

                      • gits
                        Recognized Expert Moderator Expert
                        • May 2007
                        • 5390

                        #12
                        i think it has to work with all browsers ... its simply using the scope! ... you define your function in the window-context and you may ask the window for the function as its property ... you may consider the window as an array of methods ;)

                        kind regards

                        :: while it's an object of course :)

                        Comment

                        • pbmods
                          Recognized Expert Expert
                          • Apr 2007
                          • 5821

                          #13
                          Heya, andho.

                          Have a look at this article.

                          Comment

                          Working...