Esta parte del tutorial tiene como target cubrir JavaScript «tal como está», sin ajustes específicos del entorno. Pero aún utilizamos un browser how entorno de demostración. Así que deberíamos saber al menos algunas funciones de user interface. En este capítulo nos familiarizaremos con las funciones del navegador alert, prompt and confirm.
Alert
Syntax:
alert (message);
This displays a message and stops
ne la ejecución de la secuencia de comandos hasta que el Username presione «OK».
For example:
alert ("Hello");
The mini-window with the message is called a modal window. The word "modal" means that the visitor cannot <interactuar con el resto de la página, presionar otros botones, etc., hasta que haya tratado con la ventana. En este caso – hasta que presionen «OK».
Prompt
The function prompt accepts two arguments:
result = prompt (title [, default]);
Displays a modal window with a text message, an input field for the visitor, and buttons To accept and cancel.
title // title The text to display to the visitor. default // default buttons
A second optional parameter, the initial value for the input field.
The visitor can type something in the request input field and press OK. Or they can cancel the entry by pressing the CANCEL button or pressing the Esc key.
The call to prompt returns the text of the field or null if the entry was canceled.
For example:
let age = prompt ('How old are you?', 100); alert ('You are $ {age} fulfilled!); // You are 100 years old!
The second parameter is optional. But if we don't supply it, Internet Explorer would insert the text «undefined«On the indicator.
Run this code in Internet Explorer to see that:
let test = prompt ("Test");
Therefore, to look good in IE, it is recommended to always provide the second argument:
let test = prompt ("Test", ''); // <- for IE
Confirmed
The syntax:
result = confirm (question);
The function confirm shows a modal window with the question buttons: Accept and CANCEL.
The result is true if OK is pressed and false otherwise.
For example:
let Boss = confirm ("Are you the boss?"); alert (Boss); // true if OK is pressed
In today's lesson we covered three specific functions of the browser to interact with the visitor:
- alert displays a message.
- prompt muestra un mensaje pidiendo al usuario que ingrese texto. Devuelve el texto o, si CANCELAR o Esc se presionan, todos los browsers regresan null.
- confirm displays a message and waits for the user to press "OK" or "CANCEL". Comes back true for OK and false to CANCEL or Esc.
All these methods are modal: they pause the execution of the script and do not allow the visitor to interact with the rest of the page until the message has been discarded.
There are two limitations shared by all of the above methods:
The exact location of the modal window is determined by the browser. Usually it is in the center.
The exact appearance of the window also depends on the browser. We cannot modify it.
That's the price for simplicity. There are other ways to display nicer windows and richer visitor interaction, but if "bells and whistles" don't matter much, these methods work well.