The eval () function in javascript is basically the same as the eval function in php. It is an encryption function that allows code to run using the eval function, I 'd like to explain how to use js eval (function (p, a, c, k, e, d ).
Let's take a look at some basic functions of js eval functions.
Eval () functionComputes a string and executes the JavaScript code.
Syntax
Eval (string) parameter description
String is required. The string to be calculated, which contains the JavaScript expression to be calculated or the statement to be executed.
Return Value
Returns the string value (if any ).
Description
This method only accepts the original string as the parameter. If the string parameter is not the original string, this method will be returned without any change. Therefore, do not pass a String object as a parameter for the eval () function.
If you try to override the eval attribute or assign the eval () method to another attribute and call it through this attribute, ECMAScript can throw an EvalError exception.
Throw
If the parameter does not contain valid expressions and statements, a SyntaxError exception is thrown.
If eval () is illegally called, an EvalError error is thrown.
If the Javascript code passed to eval () generates an exception, eval () will pass the exception to the caller.
Tip: Although eval () is very powerful, it is rarely used in actual use.
Eg:
The Code is as follows: |
Copy code |
<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> |
Encryption and decryption program
The Code is as follows: |
Copy code |
<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 <? '': E (parseInt (c/a) + (c = c % a)> 35? String. fromCharCode (c + 29): c. toString (36)}; 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 <? '': 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 = encoding> <Input type = button onclick = run () value = Execution> <Input type = button onclick = decode () value = decoding> |