Hey, Amazing ones
Hello everyone, my name is Aryan Sharma and I hope you're all doing well.
I'm excited to kick off this JavaScript course for absolute beginners with this blog post.
Here we go🚀...
There are three browser-specific functions to interact with visitors: alert
, prompt
, and confirm
. All these methods are modal - they pause script execution and don’t allow the visitor to interact with the rest of the page until the window has been dismissed.
alert
This one we’ve seen already. It shows a message and waits for the user to press “OK”.
Syntax:
alert(message);
This shows a message and pauses script execution until the user presses "OK". For example:
alert("Hello");
The mini-window with the message is called a modal window. The word “modal” means that the visitor can’t interact with the rest of the page, press other buttons, etc, until they have dealt with the window. In this case – until they press “OK”.
prompt
The function prompt
accepts two arguments:
title
: The text to show the visitor.default
: An optional second parameter, the initial value for the input field.
result = prompt(title, [default]);
The visitor can type something in the prompt input field and press OK. Then we get that text in the result
. Or they can cancel the input by pressing Cancel or hitting the Esc key, then we get null
as the result
.
The call to prompt
returns the text from the input field or null
if the input was canceled.
For instance:
let age = prompt('How old are you?', 100);
alert(`You are ${age} years old!`); // You are 100 years old!
⚡always supply a default
The second parameter is optional, but if we don’t supply it, Internet Explorer will insert the text "undefined"
into the prompt.
Run this code in Internet Explorer to see:
confirm
It shows a modal window with a question and two buttons: OK and Cancel. The result is true if OK is pressed and false otherwise. For example:
result = confirm(question);
The function confirm
shows a modal window with a question
and two buttons: OK and Cancel.
The result is true
if OK is pressed and false
otherwise.
For example:
let isBoss = confirm("Are you the boss?");
alert( isBoss ); // true if OK is pressed
#Tasks -
A simple page
Create a web page that asks for a name and outputs it.
Hope you loved this blog on JavaScript Interactions,
Don't forget to like and support by following.
Thanks! Have a wonderful day đź’ť