using eval() on a javascript function with onclick

Posted on 2006-06-09
Last Modified: 2008-02-26
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
Question by:kateReznik
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
LVL 63

Expert Comment

ID: 16874934
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.


Author Comment

ID: 16874970
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?
LVL 63

Expert Comment

ID: 16874989
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.

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 63

Expert Comment

ID: 16875022
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?


Author Comment

ID: 16875023
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?
LVL 63

Expert Comment

ID: 16875034
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')))"

Author Comment

ID: 16875043
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.
LVL 63

Expert Comment

ID: 16875058
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.


Author Comment

ID: 16875071
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.  
LVL 63

Accepted Solution

Zvonko earned 500 total points
ID: 16875084
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.


Author Comment

ID: 16875115
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 :)
LVL 63

Expert Comment

ID: 16875121
Send me your phone number to the email address in my profile. Do you know

Author Comment

ID: 16875151
i sent u my skype nick.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question