JavaScript eval () function introduction and application example, javascripteval

Source: Internet
Author: User
Tags javascript eval

JavaScript eval () function introduction and application example, javascripteval

The eval (String) function computes a String and executes the JavaScript code.

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.

Tips and comments

Tip: Although eval () is very powerful, it is rarely used in actual use.

Example:

Output:

200

4

24


Question about the EVAL () function in JAVASCRIPT? As follows:

Eval can execute a string, not just a variable. For example, you have nine functions, func1 ~ Func9, according to the value of variable I (1 ~ 9), decide which function to call. You need to write:
Switch (I)
{
Case 1: func1 (); break;
Case 2: func2 (); break;
...... (Write 9 rows)
}

But with eval, you only need to write one line:
Eval ("func" + I + "()");

You can understand this time.

What are the advantages of javascript eval functions? It is only known that the string in the brackets is used as a common example of JS statement processing to illustrate its purpose and advantages.

Eval can generate a string and execute a dynamic js statement.
Eval usage: Sometimes we don't know what statements to execute in advance, and we only know what statements to execute when the conditions and parameters are given. In this case, eval comes in handy.
Eval function
Function: first explain the Javascript code and then execute it.
Usage: Eval (codeString)
CodeString is a string containing Javascript statements. It is compiled using the Javascript engine after eval.

For example:
Function output (a, B)
{
Var tmpa, tmpb;
Tmpa = eval ("document. all." + a + ". value ");
// The dynamic execution of js here is equal to that of tmpa = document. getElementById (a). value;
Tmpb = eval ("document. all." + B + ". value ");
// Dynamically execute js
// The dynamic execution of js is equivalent to tmpb = document. getElementById (B). value;
Document. write (tmpa + tmpb );
}
Output ('input1', 'input2 ');

Example 2:
Function tophide (id) // id indicates menu
{
If (top. topframeset. rows = "31 ,*")
{
Top. topframeset. rows = "86 ,*";
Eval (id + "_ icon. src ="/imgs/collapse_up.gif "); // dynamically execute js

Eval (id + "_ icon. alt = 'collapse The head'"); // dynamically execute js
Head. style. display = "block"
}
Else
{
Top. topframeset. rows = "31 ,*";
Eval (id + "_ icon. src ="/imgs/collapse_down.gif "); // dynamically execute js
Eval (id + "_ icon. alt = 'expand The He... The remaining full text>

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.