| by Arround The Web | No comments

Window confirm() Method in JavaScript

The window object in JavaScript is how JavaScript interacts and communicates with the browser. JavaScript uses the browser object model which is used to interact with the different components of the browser i.e., navigation, the width, the height of the browser window. The window object comes with several built-in properties and methods which are useful for JavaScript developers as these can be used to manipulate the browser window. One of these methods is the confirm() method which we will explain today in this write-up.

The window confirm() method is used to prompt a user with a message and get their response. The confirm() method opens a pop-up on top of the browser window, displays a text message and two buttons, the OK and the Cancel button which are used to get the response of the user. The confirm() method blocks the user from accessing the website until they have provided a response. This feature can come in handy in some cases but developers are recommended not to overuse the confirm() method and instead use its alternatives like the alert() method.

confirm() method Syntax

The confirm() method is called with the reference of the window object and takes a string literal as an argument. This string is the message that is displayed on the pop-up:

window.confirm(text);

 
But as the window object represents the global scope, its methods can be called without any reference. Thus, the below given syntax is equally valid:

confirm(message);

 

How to Use the Window confirm() Method?

Simply invoke the confirm() method and pass it a string containing the message that you want to display to the screen. I’ll be using the browser console to demonstrate the working of the confirm() method:

confirm("Press Okay to Confirm");

 

The confirm() method actually returns a Boolean value which can be stored in a variable to determine the further course of action. In case the user clicks on the OK button then the confirm() method returns true else it returns false.

var op = confirm("Press Okay to Confirm");

if(op == true)
{
    console.log("OK pressed");
}
else
{
    console.log("Cancel pressed");
}

 
Understanding the Code

We first use the confirm() method to show the user a message through a pop-up on the screen:


The user now only has two options either to click the OK or the Cancel button as the confirm() method blocks user’s access to the webpage. If the user presses OK, the confirm() method will return true. Upon pressing the Cancel button, the method will return false. We are storing these return values in our op variable.

We then use this variable in our conditional statements to print whether the user has pressed OK or Cancel button:


 

 

 

Conclusion

This beginner’s guide has an elaborate and easy explanation of how the confirm() method is used to display pop-ups in JavaScript. confirm() is one of many built-in JavaScript methods which belong to the global window object which can be used to communicate with the browser window.

The confirm() method has many real-world applications like its most frequent use is confirmation dialogs which appear when a user tries to leave or refresh a web page without saving their progress e.g., trying to leave an unfinished email on Gmail.

Share Button

Source: linuxhint.com

Leave a Reply