Take user input with JavaScript in the console

徘徊边缘 提交于 2019-12-01 21:21:36

问题


I need to get user input when running a .js in a console with spidermonkey like this:

$ js myprogram.js

What's the JavaScript equivalent of Ruby's gets?


回答1:


As far as I know, there is a readline() function, but it is a specific function for spidermonkey, it isn't a part of javascript.

Example:

1)readline-test.js:

print("Type some text and press <ENTER>:\t");
var userInput = readline();
print("User input: " + userInput);

2)js readline-test.js

For more information see https://developer.mozilla.org/en-US/docs/SpiderMonkey/Introduction_to_the_JavaScript_shell.




回答2:


You can't really depend on the console being there. It's not something that is guaranteed, even while most browsers do have a javascript console. Beyond that I don't believe you can get values without calling a function with the console.



来源:https://stackoverflow.com/questions/16307415/take-user-input-with-javascript-in-the-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!