Orion Adrian
Orion Adrian

Reputation: 19543

How can I find the location of a Javascript Syntax error when using eval?

I have a long string that I am calling eval on. It's resulting in a syntax error. I'm trying to figure out where in the eval string the syntax error is. The page is executing in IE 9.

This code string is the result of a large process and is very legacy code. The architecture can't change now as that is out of the scope of the defect I'm working on. It's not a great system and certainly not a system I would write, but it is what it is.

Upvotes: 0

Views: 128

Answers (4)

Esailija
Esailija

Reputation: 140220

You could replace the eval with console.log, run the logged string through jsbeautifier and spot the syntax error.

As in:

eval( "very huge string" )

-->

console.log( "very huge string" )

Upvotes: 2

hugomg
hugomg

Reputation: 69934

Tell your app to spit out your string before eval-ing and running into the syntax error. You can now either

  • Put that into another file and tell the browser to run that. (error messages are usually better this way)
  • Copy and paste the code into JSHint and ask it to find the syntax error for you.

Upvotes: 0

Martin.
Martin.

Reputation: 10529

It is better not to use eval. Put your code out of eval() and try again

Upvotes: 0

Quentin
Quentin

Reputation: 943470

By refactoring so you don't need to use eval, there is almost always a better way.

Upvotes: 0

Related Questions