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!

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
I say that, but then I remember this...
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.