using eval() on a javascript function with onclick

I have a javascript function

  <script type="text/_javascript">
               function popupTest(something)

I would like to call eval on this function and pass that as the onclick script for a button.
The reason I am doing this is a bit har to explain , so I would like to find out if it is possible at all.
essentially I would like to have this:
1) <input type="button" value="Test" onclick="eval(popupTest('testing'))" />

But this doesn't work althought the following button works:
2) <input type="button" value="Test" onclick="eval(alert('testing'))" />

So I am assuming I need some quotes inside eval() in my first  button example.
But when I tried calling it with double quotes., they were not being escaped although  I had the escape character:
3)<input type="button" value="Test" onclick="eval(\"popupTest('testing')\")" />
Single quotes also did nothing.

It is essential that I use eval()  . Can you offer any suggestions? And why are the double quotes not being escaped when inside eval? as in example 3?

Thanks in advance
Who is Participating?
ZvonkoSystems architectCommented:
Here you go:

"<input type='submit' " + somethingelse +
"onclick=' " + "myFunction(" + userOnclickScript  +") ' "

And a tip from me: avoid using Submit button onClick handlers. Better user form onSubmit event handler.

ZvonkoSystems architectCommented:
In HTML you cannot use same character as the String enclosing character inside the string.
There is no escaping in HTML, only in Script.

So you can do it either like this:
<input type="button" value="Test" onclick="eval('popupTest(\'testing\')')" />

Or like this:
<input type="button" value="Test" onclick='eval("popupTest(\"testing\")")' />

That all does not explain why you need to use eval() at all.

kateReznikAuthor Commented:
well basically i need my input button onclick to do this :
if (eval (somejavascriptfuntion))  {
  click on another button

*previously i mentioned an regular putton but im actually using a submit.

// i have no control over the somejavascript funtion that is passed in so that is why it would be difficult to escape the parameters it has . So i amg given  "popupTest('testing')" as a string

and the reason i do eval is because I need the script function (like my example popupTest) to excute wether it be a popupuWindow script or something else, then i need to click on an image button so that it can submit imageName.x imageName.y coord. to the server.  This is probably more info then needed.

So there is no way to pass  onclick=""  the whole if(eval(function)){... do the click} thing?
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

ZvonkoSystems architectCommented:
What about the stright forward approach:

<input type="button" value="Test" onclick="if(popupTest('testing'))" />

Or you do the "if" stuff in your function:
<input type="button" value="Test" onclick="yourFunction(popupTest('testing'))" />

And you CANNOT click an image button coordinates.

ZvonkoSystems architectCommented:
Or simply said: the eval() method makes only sense for String parameter or for functions or methods which return String return values.
So I do not see why you would first make an event handler to String, pass it thent to your function as string parameter of your function, do the eval() and decide then the next step. Why not simply pass the function return value to your function as your function parameter for the next step decision?

kateReznikAuthor Commented:
it would be nice if the first suggestion would work but if I do

The poupTest('testing') is not executed.

Regarding the second suggestion if i pass in 'popupTest' function within 'yourFunction' I do not need to add quotes around it?
ZvonkoSystems architectCommented:
Of course not. The functions are "evaluated" from the innerst to to the outer one.
So this is not a problem at all:
onClick="alert(confirm(prompt('Enter some question','here your question')))"
kateReznikAuthor Commented:
I don't know if i correctly understood you but:
the reason i can not simply pass the function retun value to my function because I am implimenting a custom button tag. and the function is a String wich is set by the user.
Inside the tag implementation i need to retrieve this onclickScript the user specified, then proceed to execute it, and after that , if the onclickScript returns true( this is why i need eval) i need to click another button.
ZvonkoSystems architectCommented:
Then you need also no quotes, because the user enetered string is already a string value.
You do not pass a String, you pass a var containing a string value.

kateReznikAuthor Commented:
okay. but inside my customButtonTag implementation the way i write out the button is just strings..
so i would have..
"<input type='submit'" + somethingelse +
"onclick=" + //this is where iw oudl have to put my function .. "/"myFunction(" + userOnclickScript  +")\""
So what happens is the userOnclickScript is a string value.. but since the whole button is written out as a string,  userOnclickScript in this case is not a variable .. it would just be another string concatenated in that place.  
kateReznikAuthor Commented:
ok thanks i will try this. So it is not possible to do: onclick='the whole function written out with the userOnclickScript'?  

i guess the reason im having difficulty understanding is b/c i keep on thinking that when myFunction will be executing  it would be going through the  'if(eval(........' statements anyways. So why can't i have all of that within the onclick quotes?

** i know that i am not using the whole submit button properly .. but i am doing this b/c this is a temporary hack that someone told me to do... for reasons beyond my control :)
ZvonkoSystems architectCommented:
Send me your phone number to the email address in my profile. Do you know
kateReznikAuthor Commented:
i sent u my skype nick.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.