In IE, when eval encounters a function, there will be a strange situation in IE. We will explain it step by step using an example:
Case 1: If there is no function in eval, directly execute:
Eval ("alert ('ss');"); // all browsers output correctly
Case 2: A function exists in eval and the function is executed immediately:
Eval ("(function () {alert ('ss') ;}) ();"); // all browsers correctly output
Case 3: There is a function in eval. Use the variable to save the function reference and call the function:
Var f = eval ("(function () {alert ('ss ');})");
F (); // error in IE: the object is missing and other browsers are normal
When eval defines a function and returns it to a variable, IE reports the following error: the object is missing. It can be seen that the function defined in eval in IE cannot be successfully returned to the eval external.
Solution: Make the function object return as an execution result:
Method 1:
Var f = eval ("(function () {return function () {alert ('ss ');}})()");
F (); // correct output by all browsers
Call an immediate function in eval. After the function is executed, a function object is returned. At this time, the reference of the function object is successfully returned to the external variable.
Method 2:
Var f = eval ("(false | function () {alert ('ss ');})");
F (); // all browsers are successfully output
This method is also used in jquery. function is returned as the execution result of or expression, and the problem can be solved successfully. Of course, the expression is not limited to the above false | function (){},All expressions can solve the problem if the function can be returned successfully:
/* And expression :*/
Var f = eval ("(true & function () {alert ('ss ');})");
F (); // all browsers output normally
/* Ternary expression :*/
Var f = eval ("(true? Function () {alert ('ss ');}:'');");
F (); // all browsers output normally
The end ..
JavaScript eval () function
Definition and usage
The eval () function computes 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.
Tips and comments
Tip: Although eval () is very powerful, it is rarely used in actual use. Function showsubmenu (SID ){
Whichel = eval ("submenu" + Sid );
If (whichel. style. Display = "NONE "){
Eval ("submenu" + Sid + ". style. Display = \"\";");
}
Else {
Eval ("submenu" + sid + ". style. display = \" none \";");
}
}
In IE, when eval encounters a function, there will be a strange situation in IE. We will explain it step by step using an example:
Case 1: If there is no function in eval, directly execute:
Eval ("alert ('ss');"); // all browsers output correctly
Case 2: A function exists in eval and the function is executed immediately:
Eval ("(function () {alert ('ss') ;}) ();"); // all browsers correctly output
Case 3: There is a function in eval. Use the variable to save the function reference and call the function:
Var f = eval ("(function () {alert ('ss ');})");
F (); // error in IE: the object is missing and other browsers are normal
When eval defines a function and returns it to a variable, IE reports the following error: the object is missing. It can be seen that the function defined in eval in IE cannot be successfully returned to the eval external.
Solution: Make the function object return as an execution result:
Method 1:
Var f = eval ("(function () {return function () {alert ('ss ');}})()");
F (); // correct output by all browsers
Call an immediate function in eval. After the function is executed, a function object is returned. At this time, the reference of the function object is successfully returned to the external variable.
Method 2:
Var f = eval ("(false | function () {alert ('ss ');})");
F (); // all browsers are successfully output
This method is also used in jquery. function is returned as the execution result of or expression, and the problem can be solved successfully. Of course, the expression is not limited to the above false | function (){},All expressions can solve the problem if the function can be returned successfully:
/* And expression :*/
Var f = eval ("(true & function () {alert ('ss ');})");
F (); // all browsers output normally
/* Ternary expression :*/
Var f = eval ("(true? Function () {alert ('ss ');}:'');");
F (); // all browsers output normally
The end ..
JavaScript eval () function
Definition and usage
The eval () function computes 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.
Tips and comments
Tip: Although eval () is very powerful, it is rarely used in actual use. Function showsubmenu (SID ){
Whichel = eval ("submenu" + Sid );
If (whichel. style. Display = "NONE "){
Eval ("submenu" + sid + ". style. display = \"\";");
}
Else {
Eval ("submenu" + sid + ". style. display = \" none \";");
}
}