Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4202
  • Last Modified:

using eval() on a javascript function with onclick

I have a javascript function

  <script type="text/_javascript">
               function popupTest(something)
               {
                  alert(something);
               }
   </script>

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
0
kateReznik
Asked:
kateReznik
  • 7
  • 6
1 Solution
 
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.



0
 
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?
0
 
ZvonkoSystems architectCommented:
What about the stright forward approach:

<input type="button" value="Test" onclick="if(popupTest('testing'))this.form.theOtherButton.click()" />

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.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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?

0
 
kateReznikAuthor Commented:
it would be nice if the first suggestion would work but if I do
if(popupTest('testing'))......

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?
0
 
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')))"
0
 
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.
0
 
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.

0
 
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.  
0
 
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.

0
 
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 :)
0
 
ZvonkoSystems architectCommented:
Send me your phone number to the email address in my profile. Do you know skype.com?
0
 
kateReznikAuthor Commented:
i sent u my skype nick.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now