简体   繁体   English

如何从 HTML 输入框运行 javascript 代码?

[英]How do I run javascript code from a HTML input box?

I am currently trying to run JavaScript code from a HTML input box for my online code compiler project.我目前正在尝试从 HTML 输入框中为我的在线代码编译器项目运行 JavaScript 代码。

function runCode(){
     let code = document.getElementById('code').value
     console.log(code)
}

I was expecting it to actually run the code instead of just logging it as a string我期待它实际运行代码而不是仅仅将它记录为一个字符串

This is good if you're just playing around and having fun learning JavaScript and whatnot.如果您只是在玩耍并享受学习 JavaScript 之类的乐趣,这很好。 This is not something you just want to put out on the web if you aren't that experienced.如果您没有那么多经验,这不是您只想在 web 上发布的内容。 If you do decide to put it on the web, make sure not to save anybody's code into a database.如果您决定将其放在 web 上,请确保不要将任何人的代码保存到数据库中。 You don't want UserA to write code which may execute on UserB's computer.您不希望 UserA 编写可以在 UserB 的计算机上执行的代码。 Given your experience level, I'd just say "don't do it".鉴于您的经验水平,我只想说“不要这样做”。 Without further ado:无需再费周折:

const userInput = 'console.log("Hello World");';

// method 1 
eval(userInput);

// method 2
new Function(userInput)()

If you want to allow experimental javascript features and other new JS features, you will need to use babel standalone to transform the code first.如果你想允许实验性的 javascript 特性和其他新的 JS 特性,你需要先使用babel standalone来转换代码。 Something like this:是这样的:

<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
const userInput = 'console.log("Hello World");';
var output = Babel.transform(userInput, { presets: ["env"] }).code;

eval(output);
new Function(output)();

Use eval()使用eval()

 run.onclick =() => { eval(editor.value) }
 <textArea id="editor">console.log('test')</textArea> <button id="run">run</button>

Are you going to run the code on html input element?您要在 html 输入元素上运行代码吗? when?什么时候? There should be at least an event.至少应该有一个事件。 You can run the function when click input or focus in, change and so on.单击输入或焦点、更改等时,您可以运行 function。 there are several solutions like onfocus, onkeypress, onkeyup, onchange, onclick有几种解决方案,如 onfocus、onkeypress、onkeyup、onchange、onclick

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用HTML输入框设置JavaScript变量? - How do I use a HTML input box to set a JavaScript variable? 如何使用 html 输入字段从 php 代码显示 javascript 数组中的列表? - How do i show a list in a javascript array from a php code using a html input field? 如何在JavaScript和HTML代码的文本框中摆脱NaN? - How do i get rid of the NaN in the text box in my JavaScript and HTML code? 需要将用户输入从提示框(Javascript)放入String变量。 我该怎么做? - Need to place user input from a prompt box (Javascript) into a String variable. How do I do so? 如何防止html / javascript代码在文本框中运行 - How do i prevent html/javascript code from running on a textbox 如何将输入框(html 文件)中的值传递给 javascript 注入文件(inject.js)? - How can I pass a value from an input box (html file) to a javascript inject file (inject.js)? 如何保存文本框中的输入,然后移至下一页? (使用Javascript / HTML) - How would I save input from a text box and then move to the next page? (Javascript/HTML) 如何为在Django中创建的for循环中重复的一组html代码运行JavaScript代码? - How do I run JavaScript code for a set of html code that repeats in a for loop created in Django? 如何在输入时从html表单输入框中获取值? - How do I grab the value from an html form input box as its being entered? 我如何运行JavaScript代码 - How do I run a javascript code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM