Home > Web Front-end > JS Tutorial > How to use prompt function in js

How to use prompt function in js

下次还敢
Release: 2024-05-06 12:12:14
Original
770 people have browsed it

The prompt function is used to obtain the text entered by the user by displaying a dialog box. When used, specify the prompt message (message) and optional default text (default) to be displayed, and store them in variables. This function returns the string entered by the user, or null if the input is cancelled. However, it should be noted that the prompt function will block execution, so use it with caution.

How to use prompt function in js

How to use the prompt function in JavaScript

What the prompt function does

The prompt function is a built-in function in JavaScript that is used to obtain user input. It displays a dialog box with a prompt message on the screen and waits for the user to enter text.

How to use the prompt function

The syntax for using the prompt function is as follows:

<code class="javascript">prompt(message, default);</code>
Copy after login
  • message: To be displayed to User prompt message.
  • default: (Optional) Default text to pre-populate in the dialog box.

Example

The following example shows how to use the prompt function to get the user's name:

<code class="javascript">let name = prompt("请输入您的姓名:");</code>
Copy after login

When the user clicks "OK", The entered text will be stored in the name variable.

Tip

  • To close the dialog box without getting input, the user can click the Cancel button.
  • prompt function always returns a string. If the user enters a non-string value, convert it to a string.
  • The prompt function can be used in any JavaScript environment, including browsers, Node.js, and other environments that support JavaScript.
  • Since the prompt function blocks execution, be careful when using it as it may cause the user interface to freeze.

The above is the detailed content of How to use prompt function in js. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template