Description of the alert, confirm and prompt methods in JavaScript. Methods alert, prompt, confirm in JavaScript Javascript close alert

There are three basic operations in JavaScript that allow you to receive data from the user for further processing in scripts. These are alert, prompt and confirm. What they are used for, how to use them and other nuances will be discussed later in this article.

alert

Used to display a modal window on the browser screen (this means that the user cannot click anything on the page until he closes this window. In this example, until he clicks “OK” in the window).

After the message contained in the alert is displayed, the execution of the script is suspended and resumed after the modal window is closed.

If the field is filled out and OK is clicked, the information entered by the user will be returned to the script.

The syntax of this command is somewhat more complicated than the previous one, since it allows you to set the text of the message to the user and the contents of the field for entering information, which will be displayed by default: result = prompt(title, default ) ; , Where

  • title – a message that will be displayed to the user in a modal window. The argument is required.
  • default – what will be displayed in the text input field by default. It is also required, because if it is not specified, it may lead to errors in some browsers. If you want to leave the information entry field empty, then simply set the prompt as follows:

    var myTest = prompt("Any info" , """);

A small example of using prompt:

var year = prompt("What year did you graduate from university?", 2008); alert("You are a graduate of " + year + " year!" ) ;

Typically, this command is used to collect data from users that is necessary for the script to continue further work.

confirm

Also represents a modal window. As you might guess from the name, it is usually used to coordinate something with the user.

This is why it is designed for interaction, it provides the user with OK and CANCEL buttons, which return the Boolean values ​​true and false to the script, respectively. Ratings: 4 (average 4 out of 5)

In this lesson, we'll learn about the window object's alert(), prompt(), and confirm() methods.

alert() method

The alert() method is designed to display a warning dialog box with the specified message and an "OK" button on the user's screen. It can be used to convey important information to the user.

window.alert(Parameter_1);

The alert() method has one required parameter - this is the text of the message that is displayed in the dialog box. This method does not return anything as a result of its execution.

For example, let’s display a warning dialog box for a site visitor when they click on a link: Go to website

confirm() Method The confirm() method of the window object is designed to display a dialog box on the user's screen with the specified message and the OK and Cancel buttons. A confirmation window can be used to ask the user for permission to perform a particular action.

var resultConfirm = confirm(Parameter_1);

This method has one parameter - this is the text of the message that will be displayed in the dialog box.

The confirm() method returns one of two values ​​as the result of its execution:

  • true if the user clicked "OK";
  • false if the user clicked Cancel or closed it.

For example, let's display in the element p with id="resultConfirm" the result of the user clicking the "OK" button in the dialog box:

var resultActionUser = confirm("User, please click on the OK button."); if (resultActionUser) ( document.getElementById("resultConfirm").innerHTML = "User, thank you for clicking the OK button"; ) else ( document.getElementById("resultConfirm").innerHTML = "User, we are disappointed in your response "; )

prompt() method

The prompt() method is designed to display a dialog box on the user's screen with a message, a text field for entering data, and "OK" and "Cancel" buttons. It is designed to prompt the user for data.

var resultPrompt = prompt(Parameter_1, Parameter_2);

This method has two parameters:

  • message that will be displayed in the dialog box. This parameter is required and contains a message that “tells” what data the user should enter in the text field;
  • the second parameter is optional and can be used to specify the initial value that will be printed in the dialog box's input field when opened.

Depending on the user's actions, the prompt() method may return the following data:

  • text value - if the input field contains data and the user clicked "OK";
  • empty line - if the input field does not contain data and the user clicked "OK";
  • null - if the user clicked "Cancel" or closed this window, it does not matter what data was entered into the text field.

Note: the dialog box that appears as a result of executing one of the alert() , confirm() or prompt() methods is modal, i.e. it blocks the user's access to the parent application (browser) until the user closes the dialog box.

For example, let's ask the user for a name and, depending on the result, display the text in the element with id="nameUser" :

var nameUser = prompt ("Enter your name?"); if (nameUser) ( document.getElementById("nameUser").innerHTML = nameUser +", welcome to the site!"; ) else ( document.getElementById("nameUser").innerHTML = "Guest, welcome to the site!" ; )

For example, let's ask the user to guess the number 8:

function guessNumber() ( var findNumber = prompt ("Guess a number from 1 to 10?"); switch (findNumber) ( case "6": alert("It's already warmer!"); break; case "7": alert(" It's hot!"); break; case "8": alert("You guessed right! It's the number 8."); break; case "9": alert("It's already warmer!"); break; default: alert("It's cold! "); break; ) ) ... Guess the number

And again, I welcome you to another topic dedicated to the JavaScript language, in which we will analyze the alert, prompt, confrim methods. These methods are built into the Javascript language and help us interact with the user.
Alert displays a window on the browser screen with certain information, which pauses the script until the user clicks OK.
Prompt typically displays a window that asks the user a question that they must answer in a specific text field before clicking OK. The user can also enter nothing by pressing the Cancel key.
Confirm also displays a window in which the user can no longer enter anything into the text field, but can only click OK or cancel.
And now, after a short introduction, let’s move on to considering all of the above in practice.



alert, prompt, confirm



alert("Hello, dear user!" );
var nameUser = prompt("Your name?" , "name" );
var userAnswer = confirm("Are you sure you want to leave the site?" );



As a result, when we refresh the browser page, we will see a window welcoming the user. After clicking OK, the following window will appear asking for your name. This method has two parameters, the first is required and is responsible for the title that will be displayed, in our case it is a username question. And the second parameter is responsible for the value that will be displayed by default in the text field. If you enter your name and click OK, your name will be placed in the nameUser variable. If you click the cancel button, null will be written to the variable.
And finally, a window that asks the user whether he wants to leave our site or not. In case of consent, the Boolean value true will be placed in the variable, and in case of refusal, false accordingly. That's all you need to know about these methods, see you in the next lessons!

Share with friends or save for yourself:

Loading...