0

I'm attempting to create a script which will run some JavaScript code which the user will input via an HTML input field. The effect will essentially be the same as opening the dev console and pasting your code in there, but I'd like to make it more friendly for those who are unfamiliar with the dev console.

When the user inputs his JavaScript, and presses submit, I have the input stored as a variable. What I need to do is take the content of that variable, and run it inside the browser as though it's actual code. Is it possible to do that, and how would I accomplish it?

Drew1200
  • 1
  • 1
  • possible duplicate of [Execute JavaScript code stored as a string](http://stackoverflow.com/questions/939326/execute-javascript-code-stored-as-a-string) – veysiertekin Jun 20 '14 at 22:19

2 Answers2

0

You can achieve this using eval:

var code = "alert('ok')";
eval(code);

Not that you should be very careful when doing this, since running third party code is always dangerous.

Thomas Eschemann
  • 995
  • 7
  • 18
0

You can use eval - http://www.w3schools.com/jsref/jsref_eval.asp

But is this more friendly?

Ed Heal
  • 59,252
  • 17
  • 87
  • 127