Run code javascript: How to design an environment that runs the user-provided Javascript code?

Asked

Viewed 58 times

0

I am doubtful on how to execute a code in javascript, provided by the user of the platform, as in the example of the following site: https://js.do/

1 answer

0


You can use the command eval('expressão') executing the expression passed as string (text). https://www.w3schools.com/jsref/jsref_eval.asp

However, the site you indicated uses the command

var doc = document.getElementById('result').contentDocument;
doc.open();
doc.write(code);
doc.close();

Where the element result is a iframe. That is, it opens the iframe document, writes the user’s typed content (javascript) and closes the document. This way the browser reloads the iframe with new inserted content. https://www.w3schools.com/jsref/met_doc_open.asp

  • I did some tests with the Eval() command, and managed to get results, it worked well on my project. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.