Call JavaScript function named in a string variable - how?

Last post 07-09-2008 9:32 AM by BlackCatBone. 4 replies.

Sort Posts:

  • Call JavaScript function named in a string variable - how?

    07-08-2008, 5:26 PM

    Greetings,

     I need to call JavaScript function "B" based on a string value passed to JavaScript function "A".  JavaScript function "B" resides in an external library and is not present on the page where function "A" exists.  What is the syntax to do this?

    Thanks,

    BCB

  • Re: Call JavaScript function based on (string) variable value - how?

    07-08-2008, 6:15 PM
    • Loading...
    • AceCorban
    • Joined on 08-23-2007, 3:43 PM
    • Monterey, CA
    • Posts 536

     

    //--------other library
    function B()
    {
        //do stuff
    }
    //--------end other library
    
    function A()
    {
         //do stuff
         B();
    }
     The only reason I can forsee why you might get an error is if the other library isn't loaded first.  Function A will complain that it can't find Function B if it is declared after function A is called.
    I never lose, some people are just better than me at winning.
  • Re: Call JavaScript function based on (string) variable value - how?

    07-08-2008, 7:14 PM

     Try and explain your situation a little better. Probably with an examle of the code you are using. If you need to reference a function in an external library, always put your Javascript library references first; before your own javascript. That way you can be sure that the functions are accessible. If you for some reason can't do this, usually you can just add the function to your window.onload. eg.

    window.onload = function(){
        A();
    };

  • Re: Call JavaScript function based on (string) variable value - how?

    07-09-2008, 8:39 AM

    Thanks for the replies.  I agree that I did not explain my situation well.  I'll try to do better.  Here goes:

     We have external libraries of JavaScript functions.  We have an aspx page (page "A") that contains a generalized JavaScript function that calls another JavaScript in one our several external JavaScript libraries.  Here's the twist:  The name of the JavaScript function to be called is passed as a string variable to the JavaScript function on page "A".  I don't know how to generate a valid function call to an external library starting with a variable that holds the function name.

    This is a simplified version on the JavaScript on page "A".

    function CallExternal(functionName)
    {
    // here I need to call the function named in the functionName parameter.  functionName is a string.
    }

    I need to replace the comment in the function with the correct syntax.

    Thanks!

  • Re: Call JavaScript function based on (string) variable value - how?

    07-09-2008, 9:32 AM
    Answer

    This is my solution.  The code fragment shows how the eval function was used to form a function call.  The code checks for the existence of the js function before calling it.  It also passes two string parameter values.

     targetPageScript = the name of the JavaScript in the external library

    parm1 = the value of the 1st string parameter

    parm2 = the value of the 2nd string parameter

     

                if (eval('typeof(' + targetPageScript + ')') == 'function')
                {
                    eval(targetPageScript + '("' + parm1  + '", "' + parm2 + '");');
                }
                else
                {
       	        alert('JavaScript function "' + targetPageScript + '" could not be found.  No attempt was made to call the function.');
                }
    
     
Page 1 of 1 (5 items)
Microsoft Communities
Page view counter