24 C
Jaipur
Wednesday, November 25, 2020

Javascript Confirm Method – Linux Hint

Must read

Meet DevTerm: An Open Source Portable Linux Terminal For Developers

You may be familiar with Clockwork company, which earlier launched an open-source Linux-powered portable game console called GameShell for gamers.Now, they’re back with another...

Spotify Hack – Over 300k Accounts Hacked in Credential Stuffing Attack

Spotify is a Swedish-based audio streaming and media services provider, with over 299 million active monthly users in 2020. Noam Rotem and Ran Locar, vpnMentor’s research team...

Javascript is the most known language of the web. Javascript is widely used in front-end development as well as in the back-end. Javascript provides a lot of built-in objects, functions, and methods to help in web development. In this article, we are going to learn one of the javascript’s built-in confirm() method, which is used to show pop-ups over the screen and get the user’s response. The confirm box is a bit different if we try to compare it with the alert box. It is a pop-up that contains a message/text with two buttons, “OK” and “Cancel”. The user won’t be able to do any task while a confirm box is over the screen, and he/she clicks the “OK” or “Cancel” button. This is the reason behind not recommending it’s often used. So, let’s have a look at what is a confirm box and what are the different ways to use it.

The confirm() is basically a method, which is used to show a pop-up box over the web page, and it contains a message or text and two buttons, “OK” & “Cancel”. On the click of the “OK” button, the confirm method returns “true”. Similarly, on the click of the “Cancel” button, it returns false.

Syntax

There are two different syntaxes for showing the confirm box. One of them is using the window’s object

But, we can use the confirm() method without the window’s object as well.

In this syntax, the message can be any string or a variable that can include a message.
So, let’s try both of the syntaxes.

Examples

First, let’s try with the window’s object

window.confirm(“Confirm message from Linuxhint”);

And now without window’s object

confirm(“Confirm message from Linuxhint”);

You will witness that there is no difference in both of them.

The confirm method doesn’t only take the string to show the message. We can provide variable as well, and it worked perfectly fine.

var confirmMessage = Confirm Message using variable‘;
confirm(confirmMessage);

As you can see in the screenshot below that the message is displayed.

We have learned about providing a variable as well. What if we want to show the pop-up alert box on the screen at the click of a button. For example, we have got some information from the user, and after successfully saving the user’s data on the server, we want to show a confirmation message that “Confirmed”. So, we can simply show a confirmation box like this.


Or if we are getting a confirmation message from the server, and we want to show the message on the base of the message we got. We can call the function on the button’s onClick method.

And later in the script, we can write the function in which we can show the confirmation message.

function confirmFunc() {
 var confirmMessage = ‘Confirm Box using function’;
 confirm(confirmMessage);
}


So, these are some of the different methods of using the confirm() method.

Conclusion

In this article, we have learned about the javascript’s built-in confirm method to show pop-up over the browser’s window. This article has explained the use of the confirm method in a very easy, profound, and effective way that any beginner can understand it and use it. So, keep on learning, working, and getting experience in javascript with linuxhint.com to have a better grasp over it. Thank you so much.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

Meet DevTerm: An Open Source Portable Linux Terminal For Developers

You may be familiar with Clockwork company, which earlier launched an open-source Linux-powered portable game console called GameShell for gamers.Now, they’re back with another...

Spotify Hack – Over 300k Accounts Hacked in Credential Stuffing Attack

Spotify is a Swedish-based audio streaming and media services provider, with over 299 million active monthly users in 2020. Noam Rotem and Ran Locar, vpnMentor’s research team...