PDA

View Full Version : eval("")


denis
09-21-2008, 06:10 PM
I am trying to use eval and its not working like its suppose to .

o1,o2 and o3 are operands number
where as oper1 and oper2 are operators *, /, %, +, -

var out = eval("(o1);oper1;(o2);oper2;(o3)");
alert(out);
so when i use this i get the whatever stored in o3 as a result.

help will be greatly appreciated.
Denis

RysChwith
09-22-2008, 08:27 AM
JavaScript doesn't do in-string variable replacement. You'd want something like this:var out = eval( o1 + oper1 + o2 + oper2 + o3 ); //this assumes that all variables are strings
alert( out );Note that eval() should be used as sparingly as possible; it may be worth looking into other ways to accomplish this, depending on what you're doing. At the very least, make sure to sanitize your inputs.

Rys