Solved

using eval() on a javascript function with onclick

Posted on 2006-06-09
13
3,533 Views
Last Modified: 2008-02-26
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
Comment
Question by:kateReznik
  • 7
  • 6
13 Comments
 
LVL 63

Expert Comment

by:Zvonko
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.



0
 

Author Comment

by:kateReznik
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?
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16874989
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
 
LVL 63

Expert Comment

by:Zvonko
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?

0
 

Author Comment

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

Expert Comment

by:Zvonko
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')))"
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:kateReznik
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.
0
 
LVL 63

Expert Comment

by:Zvonko
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.

0
 

Author Comment

by:kateReznik
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.  
0
 
LVL 63

Accepted Solution

by:
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.

0
 

Author Comment

by:kateReznik
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 :)
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16875121
Send me your phone number to the email address in my profile. Do you know skype.com?
0
 

Author Comment

by:kateReznik
ID: 16875151
i sent u my skype nick.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Javascript loop through list 13 34
How can I remove this CSS class from div? 5 29
HTML page and JavaScript 2 54
onOpen 14 43
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now