Recently I was helping some of my friends who were trying to execute a JavaScript code programmatically. The requirement was that the user can enter a JavaScript code in a textarea and click a button and the user should get the output on the screen. They had taken a longer route: take the code and send it to the server through ajax. Paste the code in a script tag of a dummy page and display the page as an iframe. Wow!
So I pitched in and suggested a simpler solution. Take the code and pass it to the eval(). Ensure you have a try,catch block to capture error messages as well. Worked like a charm! Of course, we had to ‘disable’ a few things in it like alert, prompts, etc. It was fun!