JavaScript Tutorial: Interaction - Alert, Prompt,
Confirm |
Web Development Tutorials
How to Make a Page Interactive Using Alert, Prompt, and Confirm
In this tutorial, we will explore how to make a web page interactive using
JavaScript. Specifically, we will learn how to use the alert, prompt, and
confirm functions. These functions are essential for creating user
interactions on a website.
Alert Function
The alert function is used to display a message to the user. It opens a
modal window with the specified message.
To use the alert function, simply call it and pass a string as an argument.
For example:
alert("This is a message");
When the code above is executed, a modal window with the message "This
is a message" will be displayed on the webpage.
It's important to note that the alert function does not return anything.
Prompt Function
, The prompt function is used to ask the user for input. It opens a modal
window with a message and an input field for the user to enter their
response.
To use the prompt function, call it and assign the user's input to a variable.
For example:
let name = prompt("What is your name?", "Guest");
In the code above, the prompt function will display the message "What is
your name?" and provide a default value of "Guest" in the input field. The
user's input will be stored in the variable "name".
It's important to note that the prompt function returns the user's input as a
string.
Confirm Function
The confirm function is used to ask the user for confirmation. It opens a
modal window with a message and two buttons: "OK" and "Cancel".
To use the confirm function, call it and assign the result to a variable. For
example:
let deletePost = confirm("Do you really want to delete this post?");
In the code above, the confirm function will display the message "Do you
really want to delete this post?" and wait for the user to click either "OK" or
"Cancel". The result of the confirmation will be stored in the variable
"deletePost".
Confirm |
Web Development Tutorials
How to Make a Page Interactive Using Alert, Prompt, and Confirm
In this tutorial, we will explore how to make a web page interactive using
JavaScript. Specifically, we will learn how to use the alert, prompt, and
confirm functions. These functions are essential for creating user
interactions on a website.
Alert Function
The alert function is used to display a message to the user. It opens a
modal window with the specified message.
To use the alert function, simply call it and pass a string as an argument.
For example:
alert("This is a message");
When the code above is executed, a modal window with the message "This
is a message" will be displayed on the webpage.
It's important to note that the alert function does not return anything.
Prompt Function
, The prompt function is used to ask the user for input. It opens a modal
window with a message and an input field for the user to enter their
response.
To use the prompt function, call it and assign the user's input to a variable.
For example:
let name = prompt("What is your name?", "Guest");
In the code above, the prompt function will display the message "What is
your name?" and provide a default value of "Guest" in the input field. The
user's input will be stored in the variable "name".
It's important to note that the prompt function returns the user's input as a
string.
Confirm Function
The confirm function is used to ask the user for confirmation. It opens a
modal window with a message and two buttons: "OK" and "Cancel".
To use the confirm function, call it and assign the result to a variable. For
example:
let deletePost = confirm("Do you really want to delete this post?");
In the code above, the confirm function will display the message "Do you
really want to delete this post?" and wait for the user to click either "OK" or
"Cancel". The result of the confirmation will be stored in the variable
"deletePost".