How does one use "prompt" and "alert" javascript commands in webstorm? - Stack Overflow

admin2025-04-18  1

When i try to use prompt or alert to get user input in webstorm, I get an error:

   answers[i] = prompt(allQuestions[i].question)
                ^

ReferenceError: prompt is not defined

As far as I know, prompt and alert are fine functions in javascript. Could it be because I am running the code in the console? If so, how do I set variables to user inputted strings (i.e. prompt function)? If not, what's going wrong and how do I fix it?

When i try to use prompt or alert to get user input in webstorm, I get an error:

   answers[i] = prompt(allQuestions[i].question)
                ^

ReferenceError: prompt is not defined

As far as I know, prompt and alert are fine functions in javascript. Could it be because I am running the code in the console? If so, how do I set variables to user inputted strings (i.e. prompt function)? If not, what's going wrong and how do I fix it?

Share Improve this question edited Oct 21, 2014 at 16:00 Goodword asked Oct 17, 2014 at 3:21 GoodwordGoodword 1,64520 silver badges28 bronze badges 2
  • 1 What do you mean by "in webstorm" ? Is this in HTML page ? My guess is that you don;t have the "window" object. prompt is a function on the window object. This can happen when your programming environment is nodejs etc. – bhantol Commented Oct 17, 2014 at 3:36
  • @bhantol probably hit the nail on the head, im guessing you don't have the global window object available in the IDE. You need the window to trigger window functions. If you add prompt('test') into your browser console it will work as expected. PS don't use an IDE for JS you have everything you need in any browser and you'll never see a JS developer using an IDE unless they are working on backend code thats non-javascript. – Josh Bedo Commented Oct 17, 2014 at 13:54
Add a ment  | 

1 Answer 1

Reset to default 5

By running code in console, do you mean that you run it with Node.js? But 'prompt' (as well as 'alert', etc.) can't be used in server-side scripts executed by Node.js. You can only prompt the user with client-side javascript, i.e. running in the browser via a tag in the rendered HTML, not the Javascript API engine running on the server.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744969247a277388.html

最新回复(0)