开发者

Using JavaScript code entered into a textarea

开发者 https://www.devze.com 2022-12-30 05:52 出处:网络
I want to crea开发者_StackOverflow中文版te a <textarea>, and I want to use JavaScript code which is entered into the <textarea>, as code.

I want to crea开发者_StackOverflow中文版te a <textarea>, and I want to use JavaScript code which is entered into the <textarea>, as code.

How can I do it?


If you mean that you want to have the contents of the <textarea> parsed and evaluated as Javascript, you'd do something like this:

var script = document.getElementById('theIdOfTheTextarea').value;
eval(script);

You'd probably want to wrap that in a try/catch so that you could display an error:

try {
  eval(script);
}
catch (e) {
  alert("Error in the codes: " + e);
}


You can run the value of the textarea through the javascript eval() function, causing them to be evaluated as javascript.

Online example: http://jsbin.com/ohuqa/edit

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号