Let's take a look at some basic features of the JS eval function
The eval () function computes a string and executes the JavaScript code in it.
Grammar
eval (string) parameter description
String required. The string to evaluate, which contains the JAVASCRIPT expression to evaluate or the statement to execute.
return value
The value, if any, obtained by calculating the string.
Description
The method accepts only the original string as a parameter, and if the string argument is not the original string, the method returns without any changes. Therefore, do not pass a String object as an argument for the eval () function.
If you attempt to overwrite the Eval attribute or assign the eval () method to another property and call it through this property, the ECMAScript implementation allows a Evalerror exception to be thrown.
Thrown
Throws an SyntaxError exception if there are no valid expressions and statements in the argument.
If Eval () is invoked illegally, a Evalerror exception is thrown.
If the Javascript code passed to eval () generates an exception, eval () passes the exception to the caller.
Tip: Although the eval () is very powerful, it is not much used in practice.
eg
The code is as follows
<script language= "JavaScript" >
function Showsubmenu (SID)
{
Whichel = eval ("submenu" + SID);
if (WhichEl.style.display = "None")
{
Eval ("submenu" + Sid + ". style.display=" ";");
}
Else
{
Eval ("submenu" + Sid + ". style.display=" None ";");
}
}
</SCRIPT>
Cryptographic Decryption Program
The code is as follows
<script>
a=62;
function encode () {
var code = document.getElementById (' code '). Value;
Code = code.replace (/[rn]+/g, "");
Code = code.replace (/'/g, "\");
var tmp = Code.match (/b (w+) b/g);
Tmp.sort ();
var dict = [];
var i, t = ';
for (var i=0; i<tmp.length; i++) {
if (Tmp[i]!= t) dict.push (t = tmp[i]);
}
var len = dict.length;
var ch;
For (i=0 i<len; i++) {
ch = num (i);
Code = code.replace (New RegExp (' \b ' +dict[i]+ ' \b ', ' g '), CH);
if (ch = = Dict[i]) dict[i] = ';
}
document.getElementById (' Code '). Value = ' eval (function (p,a,c,k,e,d) {e=function (c) {return (c<a? '): E (parseint a)) + ((c=c%a) >35? String.fromCharCode (c+29): C.tostring ())};if (! "). Replace (/^/,string)) {while (c--) d[e (c)]=k[c]| | E (c); K=[function (e) {return d[e]}];e=function () {return ' \\w+ '};c=1};while (c--) if (k[c)) P=p.replace (New RegExp (' \\b ') +e (c) + ' \\b ', ' G '), k[c]); return p} ("
+ "' +code+" ', "+a+", "+len+", ' "+ dict.join (' | ') + "'. Split (' | '), 0,{})";
}
function num (c) {
Return (c<a? ': num (parseint (C/A)) + ((c=c%a) >35? String.fromCharCode (c+29): c.tostring (36));
}
function Run () {
Eval (document.getElementById (' Code '). Value);
}
function decode () {
var code = document.getElementById (' code '). Value;
Code = code.replace (/^eval/, "");
document.getElementById (' Code '). Value = eval (code);
}
</script>
<textarea Id=code cols=80 rows=20>
</textarea><br>
<input Type=button Onclick=encode () value= Coding >
<input Type=button Onclick=run () value= Executive >
<input Type=button Onclick=decode () value= decoding >