0

I need a function in my currently building web project. Users are able to input their own Javascript code into <textarea> and run it after clicking a <button>. I have no idea about how to implement it. Could anyone help me with that? Thanks!

Neo
  • 2,196
  • 5
  • 32
  • 58

1 Answers1

1

The most straight forward way that I can think to do this would be to use the eval() function. You would read the input code from the <textarea> and then pass it to eval(codeString) which would execute it.

With the above said there are security risks with using eval and there are probably better (more complex) ways to do what you want but for something simple I think eval() could work for you.

EDIT: So long as you're only running code this particular user gave you, and aren't letting users run other people's code, there is no security risk - @Niet the Dark Absol

secretformula
  • 6,414
  • 3
  • 33
  • 56
  • 2
    So long as you're only running code this particular user gave you, and aren't letting users run other people's code, there is no security risk because it's basically a primitive Developer Tools console. – Niet the Dark Absol May 15 '14 at 16:54
  • I say that, but then I remember [this](http://stackoverflow.com/questions/21692646/how-does-facebook-disable-the-browsers-integrated-developer-tools)... – Niet the Dark Absol May 15 '14 at 16:56
  • I think in the general case you are correct, you arent really giving the (malicious)user anything that they didnt have before with this kinda thing on the type of project I think the OP will be creating – secretformula May 15 '14 at 16:58