<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
const input = prompt('message', '_default')
alert(input)
</script>
</head>
<body>
</body>
</html>
1. **`prompt('message', '_default')`**: This line displays a dialog box to the user with a text input field.
- The first argument `'message'` is the text displayed in the dialog box as a message or question to the user.
- The second argument `'_default'` specifies the default input value that appears in the text field. The user can either accept this default value, modify it, or enter a completely new value.
- The `prompt` function returns the input value as a string when the user clicks "OK". If the user clicks "Cancel", it returns `null`.
2. **`alert(input)`**: This line then displays an alert dialog box showing the value entered or the default value if the user directly clicked "OK" without making changes. If the user clicked "Cancel", the alert will show `null`.
### How It Works:
- Upon visiting a page containing this script, the user is immediately prompted with a dialog box asking for input, displaying the message "message" and providing "_default" as the pre-filled suggestion in the input field.
- After the user responds to the prompt and clicks "OK", an alert dialog box displays the user's response. If the user chooses to cancel the operation, the alert shows `null`.
### Use Case:
This pattern is particularly useful for web applications requiring quick input or confirmation from users, such as entering a name, email, or other simple data, before proceeding with an operation. It's a straightforward method to engage users and gather necessary information without requiring complex forms or interfaces.
'개념 > 혼자 공부하는 Javascript' 카테고리의 다른 글
function) apply() (0) | 2025.01.28 |
---|---|
function) confirm() (0) | 2025.01.27 |
class) generate object (0) | 2025.01.25 |
method) private (1) | 2025.01.24 |
class) Ractangle class (0) | 2025.01.23 |