Evaluates JScript code and executes it.
function eval(codeString : String [, override : String]) |
Arguments
- codeString
-
Required. A string that contains valid JScript code.
- override
-
Optional. A string that determines which security permissions to apply to the code in codeString.
Remarks
The eval function allows dynamic execution of JScript source code.
The code passed to the eval method is executed in the same context as the call to the eval method. Note that new variables or types defined in the eval statement are not visible to the enclosing program.
The code passed to the eval method is executed in a restricted security context, unless the string "unsafe" is passed as the second parameter. The restricted security context helps to prevent access to system resources, such as the file system, the network, or the user interface. A security exception is generated if the code attempts to access those resources.
When the second parameter of eval is the string "unsafe", the code passed to the eval method is executed in the same security context as the calling code. The second parameter is case sensitive, so the strings "Unsafe" or "UnSAfE" will not override the restricted security context.
Security Note |
---|
Use eval in unsafe mode only to execute code strings obtained from trustworthy sources. |
Example
For example, the following code initializes the variable mydate
to a test date or the current date, depending on the value of the doTest
variable:
В | Copy Code |
---|---|
var doTest : boolean = true; var dateFn : String; if(doTest) dateFn = "Date(1971,3,8)"; else dateFn = "Date()"; var mydate : Date; eval("mydate = new "+dateFn+";"); print(mydate); |
The output of this program is:
В | Copy Code |
---|---|
Thu Apr 8 00:00:00 PDT 1971 |