executing a function from string text

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Merrill Higginson

    executing a function from string text

    I have an application that navigates through the links in a document,
    most of which are "javascript:doT hisFunction(arg s)" type of links.
    Using DOM navigation I can find the reference to the javascript
    function and store it as a String variable, but is there a way to
    execute the function? Currently the only way I know how to do it is
    in a series of if/else statements as in:

    if (link=="doThisF unction(args)")
    doThisFunction( args);
    else if (link=="doThatF unction(moreArg s)")
    doThatFunction( moreArgs);
    ... etc.

    is there a more elegant way to do this?

    Merrill
  • Thomas 'PointedEars' Lahn

    #2
    Re: executing a function from string text

    Merrill Higginson wrote:
    [color=blue]
    > I have an application that navigates through the links in a document,
    > most of which are "javascript:doT hisFunction(arg s)" type of links.[/color]

    <http://jibbering.com/faq/#FAQ4_24>
    [color=blue]
    > Using DOM navigation I can find the reference to the javascript
    > function and store it as a String variable, but is there a way to
    > execute the function?[/color]

    You already "execute" the function as the expression within the
    "javascript :" URI is evaluated.
    [color=blue]
    > Currently the only way I know how to do it is
    > in a series of if/else statements as in:
    >
    > if (link=="doThisF unction(args)")
    > doThisFunction( args);
    > else if (link=="doThatF unction(moreArg s)")
    > doThatFunction( moreArgs);
    > .. etc.
    >
    > is there a more elegant way to do this?[/color]

    It is unclear what you intend to do. However, using event handler
    attributes instead, as of DOM Level 0 (proprietary) you would be
    able to call the event handler using its lowercased attribute name,
    once you have a reference to the respective DOM element object.


    HTH

    PointedEars

    Comment

    • Robert

      #3
      Re: executing a function from string text

      bookman2@hotmai l.com (Merrill Higginson) wrote in message news:<8b5f6d8.0 408111956.40662 ce6@posting.goo gle.com>...
      [color=blue]
      > if (link=="doThisF unction(args)")
      > doThisFunction( args);
      > else if (link=="doThatF unction(moreArg s)")
      > doThatFunction( moreArgs);[/color]
      [color=blue]
      > is there a more elegant way to do this?[/color]


      I am not sure I would call this elegant since most people refer to
      eval as the evil eval function. Try:

      eval(link);

      Robert

      Comment

      Working...