How to Create and Retrieve Data from a Confirm Dialog Box in Javascript


Javascript



In this article, we show how to create and retrieve data from a confirm dialog box with Javascript.

A confirm dialog box is a message box that comes up automatically when a user reaches a website (of a website that contains this feature) which asks the user a certain question; based on the user's answer (by either clicking "Ok" to agree or "Cancel" to disagree) will the program respond according, to how it is programmed to run based on the answer the user provides.

For example, a confirm dialog box may pop up on a page, asking the user, "Do you agree with the website private policy?" If the user does by clicking "OK", he will be allowed access to the next page in the site. If he doesn't and clicks "Cancel", he will be blocked access to the next page in the site.

Below is a typical confirm dialog box that may be found on the web:


Confirm Dialog Box


Confirm dialog boxes just have "OK" and "Cancel" buttons and nothing else.

The user presses "OK" to agree to the statement at hand or presses "Cancel" to disagree.


How to Create a Confirm Dialog Box with Javascript

In javascript, now we will code a confirm dialog box, "Do you agree to the website policy?"

To do this, we use the confirm method using the following format:

confirm(message);

where message is the text we want to place in the box, such as "Do you agree with the website policy?"

So to create a confirm dialog box that asks, "Do you agree with the website policy?", the code would be:



It is best to assign the confirm method to a variable, so later when we need to retrieve information from the dialog box that the user entered, we can use that variable to reference it. So it is best to use the line:

var variable_name= confirm(message);

Example

var response= confirm ("Do you agree to the website policy?");

Return Values of the Confirm Method

The prompt box has two buttons that a user can click. "Ok" for the user to agree with the question that is stated. Or "Cancel" for a user to not agree to the stated question.

If the user clicks "Ok", then the value that is returned to the response variable is true.

If the user clicks "Cancel", then the response which is returned to the response variable is false.

The webmaster can then decide what will happen next by the user agreeing or disgreeing to the stated question. The webmaster can send any alert box s/he wants or redirect a user to different URLs based on the response. For example, if the user disagrees, he can block the user from going further into the site.

We discuss this more below about retrieving data to be able to do something more useful with it.


How to Retrieve Data from a Confirm Dialog Box with Javascript

Now we will show how to retrieve the data that the user sends us from the confirm dialog box, so that we can decide what will happen based on the user's response.

We will create an if-else situation that if the user agrees, we will send an alert box that states he has full access to all the features on the site. If the user disagrees, we will send an alert box that states, that he may proceed but will not have access to all the features on the site.

The following code below is the code that both creates and sends the alert boxes in succession to the user's response:





The first line creates the confirm dialog box and assigns it to the variable response. The answer that the user gives will be stored in this variable.

The second line is an if statement. If the user clicks "OK", then the response is true. He will be sent an alert box that states that he will have full access to all features on the website. If the user clicks "Cancel", then the response is false. And the alert box is created that states that the user will only have limited access to features on the website.


HTML Comment Box is loading comments...