Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with IE9 javascript eval()

Is there any reason for which

eval("(function(x){return x*x;})")

doesn't return a function with IE9?

like image 429
6502 Avatar asked Jul 24 '11 15:07

6502


People also ask

Is eval unsafe?

eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension.

Is eval deprecated?

The eval server command has been deprecated since MongoDB 3.0 and is definitely not recommendable for performance or security reasons.

What is the point of eval?

Answer: eval is a built-in- function used in python, eval function parses the expression argument and evaluates it as a python expression. In simple words, the eval function evaluates the “String” like a python expression and returns the result as an integer.

What does eval return in JavaScript?

The return value of eval() is the value of last expression evaluated, if it is empty then it will return undefined . Example: eval("console.


4 Answers

It's a bug in the JScript parser. It shouldn't happen in IE9 unless you are in a Compatibility or Quirks mode.

IE8 misinterprets your function expression as a function declaration, causing it to fail to return anything. Previously mentioned in this question.

You can work around it using one of the other typical ways to unconfuse the JScript parser on what constitutes an expression vs a statement, eg:

eval('[function(x){return x*x;}][0]')
eval('0?0:function(x){return x*x;}')
like image 105
bobince Avatar answered Sep 30 '22 00:09

bobince


Due to a parser bug, IE's eval cannot directly evaluate a function.

Instead, you should use the Function constructor:

new Function("x", "return x * x;")

Alternatively, you can wrap the eval'd expression in an array:

eval("[ function(x){return x*x;} ][0]")

IE doesn't object to evaling an array or object that contains a function.

like image 21
SLaks Avatar answered Oct 01 '22 00:10

SLaks


eval("(function(x){return x*x;})")

...works just fine in IE9 in normal mode (live proof) (but see below). Doesn't mean it's a good idea (very, very few uses of eval qualify as anything other something than to be avoided), but it does work.

It also works (and should work) in recent versions of Chrome, Firefox, and Opera.

It doesn't seem to work in IE6, IE7, IE8, or IE9 in "compatibility view". That would be a bug (apparently one that's fixed along with about a thousand others with the newer JavaScript engine in IE9), there's nothing wrong with the expression.

You can fix it in earlier versions by forcing the parser to realize it's an expression (live copy):

eval("(0 || function(x){return x*x;})")
like image 22
T.J. Crowder Avatar answered Sep 27 '22 00:09

T.J. Crowder


I just tested it and

eval("(function(x){return x*x;})")

it returns a function in IE9.

like image 34
Norbert Tamas Avatar answered Sep 29 '22 00:09

Norbert Tamas