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?
Asked
Active
Viewed 287 times
1
-
2@AnthonyGrist What's the risk here ? – Denys Séguret May 07 '13 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" – elclanrs May 07 '13 at 10:48
-
jsFiddle does exactly this. – Chris Dixon May 07 '13 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. – Anthony Grist May 07 '13 at 10:49
-
possible duplicate of [Can you programmatically access the Firebug console output?](http://stackoverflow.com/questions/4375522/can-you-programmatically-access-the-firebug-console-output) and similar ones – Bergi May 07 '13 at 10:49
-
@Bergi Nope, I'm not going to use Firebug Console – Sazid May 07 '13 at 12:23
-
@Sazid: What else do you mean by "browser's console" then? They are debugging tools, and not accessible from the javascript environment. What do you need this for? – Bergi May 07 '13 at 12:26
-
@Bergi Well, I am actually trying to create something like Jsfiddle which will run on the client side/end user's PC. – Sazid May 07 '13 at 13:02
-
@Sazid: But that doesn't need the browser's console, does it? – Bergi May 07 '13 at 13:10
-
@Bergi Please, will you tell me, how can I achieve that then? – Sazid May 07 '13 at 13:27
1 Answers
5

Denys Séguret
- 372,613
- 87
- 782
- 758
-
You shouldn't use `eval` anywhere! It is considered as evil! Here, look at this - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/eval – akash4eva May 07 '13 at 10:44
-
Ok, but what does the eval() function is doing here? I also heard its evil-ness. – Sazid May 07 '13 at 10:45
-
eval evaluates/performs the JavaScript expressions/statements passed as argument to it. See https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/eval – Ali May 07 '13 at 10:47
-
5@akash4eva Stop propagating irrational fears. `eval` is perfectly suited to this situation. – Denys Séguret May 07 '13 at 10:49
-
2`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. – elclanrs May 07 '13 at 10:49
-
2Solid answer +1 op might also find this useful http://stackoverflow.com/questions/15409639/how-to-execute-different-partsof-the-js-code-in-one-scope/15409716#15409716 – Benjamin Gruenbaum May 07 '13 at 11:00
-
But, what about getting the output from the console? Please someone tell me! – Sazid May 07 '13 at 12:24
-
You mean you want to eval what the user types in the console ? [There's no function for that](https://developer.mozilla.org/en-US/docs/DOM/console). – Denys Séguret May 07 '13 at 12:56