executing a function from string text

M

Merrill Higginson

I have an application that navigates through the links in a document,
most of which are "javascript:doThisFunction(args)" 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=="doThisFunction(args)")
doThisFunction(args);
else if (link=="doThatFunction(moreArgs)")
doThatFunction(moreArgs);
... etc.

is there a more elegant way to do this?

Merrill
 
T

Thomas 'PointedEars' Lahn

Merrill said:
I have an application that navigates through the links in a document,
most of which are "javascript:doThisFunction(args)" 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?

You already "execute" the function as the expression within the
"javascript:" URI is evaluated.
Currently the only way I know how to do it is
in a series of if/else statements as in:

if (link=="doThisFunction(args)")
doThisFunction(args);
else if (link=="doThatFunction(moreArgs)")
doThatFunction(moreArgs);
.. etc.

is there a more elegant way to do this?

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
 
R

Robert

if (link=="doThisFunction(args)")
doThisFunction(args);
else if (link=="doThatFunction(moreArgs)")
doThatFunction(moreArgs);
is there a more elegant way to do this?


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

eval(link);

Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top