Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eval javascript, check for syntax error

I wanted to know if it is possible to find through javascript if a call to eval() has a syntax error or undefined variable, etc... so lets say I use eval for some arbitrary javascript is there a way to capture the error output of that eval?

like image 264
Jesus Ramos Avatar asked Feb 07 '11 15:02

Jesus Ramos


People also ask

What is eval error in JavaScript?

The EvalError object indicates an error regarding the global eval() function. This exception is not thrown by JavaScript anymore, however the EvalError object remains for compatibility.

What is the syntax of eval in JavaScript?

JavaScript eval()The eval() method evaluates or executes an argument. If the argument is an expression, eval() evaluates the expression. If the argument is one or more JavaScript statements, eval() executes the statements.

Why eval () is the evil?

eval() is evil if running on the server using input submitted by a client that was not created by the developer or that was not sanitized by the developer. eval() is not evil if running on the client, even if using unsanitized input crafted by the client.

Why we should not use eval in JavaScript?

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.


1 Answers

You can test to see if an error is indeed a SyntaxError.

try {     eval(code);  } catch (e) {     if (e instanceof SyntaxError) {         alert(e.message);     } } 
like image 77
ChaosPandion Avatar answered Sep 23 '22 09:09

ChaosPandion