1

How do I execute JavaScript I get from the user via a text field, inside the console and get the browser's console's output directly with JavaScript and/or jQuery?

10
  • 2
    @AnthonyGrist What's the risk here ? Commented May 7, 2013 at 10:46
  • 1
    @AnthonyGrist: It all depends on what that code is doing. I wouldn't say jsFiddle or JSBin are a "pretty big security risk" Commented May 7, 2013 at 10:48
  • jsFiddle does exactly this. Commented May 7, 2013 at 10:48
  • @dystroy Actually, having thought about it some more, if they're not saving it then it's not going to be an issue. Commented May 7, 2013 at 10:49
  • possible duplicate of Can you programmatically access the Firebug console output? and similar ones Commented May 7, 2013 at 10:49

1 Answer 1

5

You can use eval for that :

 console.log(eval('('+$('#yourInput').val()+')'))

Demonstration

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

8 Comments

You shouldn't use eval anywhere! It is considered as evil! Here, look at this - developer.mozilla.org/en-US/docs/JavaScript/Reference/…
Ok, but what does the eval() function is doing here? I also heard its evil-ness.
@akash4eva Stop propagating irrational fears. eval is perfectly suited to this situation.
eval is fine. It becomes evil when you take user input data into your application with eval. But evaluating user code with eval is totally fine.
Solid answer +1 op might also find this useful stackoverflow.com/questions/15409639/…
|

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.