Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running raw javascript (from plain text)

I'm trying to execute JavaScript from plain text (from being entered by a client). I also need a way to see if the executed code works or not (if it does, then it does, otherwise, it needs to spit out a non-variable error message).

Thanks if you can! The stuff that will be executed would be short strings such as:

echo("a","b")
like image 587
Freesnöw Avatar asked May 13 '11 17:05

Freesnöw


2 Answers

You can use eval and wrap around try-catch.

try
  {
    eval(code);
  }
catch(err)
  {
  //Handle errors here
  }
like image 76
Amir Raminfar Avatar answered Nov 13 '22 07:11

Amir Raminfar


Are you saying you just need a try/catch statement?

https://developer.mozilla.org/en/JavaScript/Reference/Statements/try...catch

like image 40
RwwL Avatar answered Nov 13 '22 06:11

RwwL