Home > Common Problem > body text

What is the usage of window.prompt?

小老鼠
Release: 2023-07-12 17:54:58
Original
1909 people have browsed it

What is the usage of window.prompt?

window.prompt() is a method in JavaScript for simple interaction with the user. It pops up a dialog box that displays a message and an input box for the user to enter text. The use of this method is very simple. This article will introduce the usage and implementation of window.prompt() in detail.

The window.prompt() method has two parameters: the first parameter is the prompt message to be displayed to the user, and the second parameter is an optional default value, which will be displayed in the input box. If the user clicks the OK button, the text value entered by the user is returned; if the user clicks the Cancel button, null is returned.

The following is a simple example that demonstrates how to use the window.prompt() method:

javascript
var name = window.prompt("请输入您的姓名:", "张三");
if (name !== null && name !== "") {
  alert("您好," + name + "!");
} else {
  alert("您没有输入姓名!");
}
Copy after login

In this example, when the page is loaded, a dialog box will pop up to prompt the user Enter name. The default value is "Zhang San". Users can enter their name in the input box and click the OK or Cancel button. When the user clicks the OK button, it will check whether the value in the input box is empty. If it is not empty, a welcome message will pop up containing the entered name; if it is empty, a prompt message will pop up indicating that the user did not enter a name.

In addition to the name input in the example, window.prompt() can also be used to obtain user input of any other text type, such as address, email, etc. You can modify and extend this example to suit your needs.

The implementation of window.prompt() is completed through the browser's dialog box function. Because this method is based on a browser dialog box, its appearance and functionality may vary from browser to browser. You need to pay attention to this when using it.

Summary: window.prompt() is a JavaScript method used for simple interaction with the user. It pops up a dialog box to display a message and an input box for the user to enter text. Use window.prompt() to easily obtain the value entered by the user and process it accordingly.

The above is the detailed content of What is the usage of window.prompt?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template