Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
44
rated 0 times [  45] [ 1]  / answers: 1 / hits: 43722  / 12 Years ago, sun, may 6, 2012, 12:00:00

I know this may be a newbie question, but I'm curious as to the main benefit of eval() - where would it be used best? I appreciate any info.


More From » eval

 Answers
4

The eval function is best used: Never.



It's purpose is to evaluate a string as a Javascript expression. Example:



eval('x = 42');


It has been used a lot before, because a lot of people didn't know how to write the proper code for what they wanted to do. For example when using a dynamic name for a field:



eval('document.frm.'+frmName).value = text;


The proper way to do that would be:



document.frm[frmName].value = text;


As the eval method executes the string as code, every time that it is used is a potential opening for someone to inject harmful code in the page. See cross-site scripting.



There are a few legitimate uses for the eval function. It's however not likely that you will ever be in a situation where you actually will need it.


[#85748] Saturday, May 5, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cristian

Total Points: 132
Total Questions: 98
Total Answers: 98

Location: Guam
Member since Tue, Nov 29, 2022
2 Years ago
;