Introduction to the Usage method of JS eval function

Source: Internet
Author: User
Tags eval

eval method

Check JScript code and execute.

Eval (codestring)

Required codestring parameter is a string value that contains valid JScript code. This string will be parsed and executed by the JScript parser.

Description
The Eval function allows for dynamic execution of JScript source code. For example, the following code creates a new variable mydate that contains a Date object:

Eval ("var mydate = new Date ();");
The context passed to the Eval method's execution is the same as the call to the Eval method

Cases

The code is as follows Copy Code

function name1 () {...}
function name2 () {...}
var m= "name1";
Eval (m+ ' () ');
Run name1 ();
m= ' name2 ';
Eval (m+ ' () ');
Run name2 ();

As a small example:

The code is as follows Copy Code
Executing an expression
var the_unevaled_answer = "2 + 3";
var the_evaled_answer = eval ("2 + 3");
Alert ("The un-evaled answer is" + The_unevaled_answer + "and the evaled answer is" + the_evaled_answer);


If you run this eval program, you will see that the string "2 + 3" in JavaScript is actually executed.
So when you set the value of The_evaled_answer to eval ("2 + 3"), JavaScript will understand and return 2 and 3 and back to The_evaled_answer.

A special note is that the object declaration syntax "{}" does not return a value, which needs to be enclosed in parentheses to return the value, as the simple example reads:

The code is as follows Copy Code

1.) str = "alert (' hello,cssrain! ')";
eval (str);//A pop-up operation is performed

2.) var kc = "{name: ' Cssrain ', email: ' Cssrain@gmail.com '}";
Eval ("cssrain=" +KC); cssrain= "{name: ' Cssrain ', email: ' Cssrain@gmail.com '}";
Alert (Cssrain)
Alert ("Name:" +cssrain.name);
Alert ("Email:" +cssrain.email);

3.) var The_unevaled_answer = "2 + 3"; Output 2+3
var the_evaled_answer = eval ("2 + 3"); Output 5
Alert ("No eval = + The_unevaled_answer +" and "eval=" + the_evaled_answer);

Example 3

Using the Eval function in JS, you can perform the JS string as a JS expression.

Save the following JS code as 1.js

  code is as follows copy code
  function f (num,callback) {                                
    //eval () function, which can execute a string as a JavaScript expression.                                         
     eval (callback+ (' +num + ') ');

Define a test.html and save to the same directory as the 1.js

The code is as follows Copy Code


<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<script src= "1.js" type= "Text/javascript" ><script>
<title> Untitled Document </title>
<script type= "Text/javascript" >
function Dosome (num) {
if (num) alert ("Not a grid!") ");
Else
if (num<=90) alert ("The student's performance is excellent!") ");
Else alert ("The student has excellent results!");
}

function Test () {
var num = document.getElementById ("Score"). Value;
F (num, "dosome");
}
</script>
<body>
<p>
eval Example:
</p>
Please input student's grade
<input type= "text" id= "score" >

<input type= "button" onclick= "Test ()" value= "See results" >

</body>

Then open test.html, and you can see that we've called the Dosome () function that passed, and the Eval function must ensure that the string representation of the JS expression is valid on the current page.

We can use a frame to execute the JS code of any accessible frame with eval. You can also call the JS code of the parent form and subform to each other by opening the subform.

And as long as we pass in the function name string (either way, rather than through the URL parameter), the caller does not need to know the name of the function called, as long as the function name string can be invoked, so more flexible.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.