We’ll be covering the following topics in this tutorial:
alert("Click OK to continue...")
The generic form of this function is alert(message). The function alert() is actually a method of the window object. It is not necessary to specify that because window is the default object. The same applies to all dialog boxes. You can also display messages using data structures. For example:
var message = "Click OK to continue" alert(message)
As you can see, the alert box is often used to pause the execution of a script until the user approves its continuation.
Confirm boxes are different from alert boxes in that they evaluate to a value based on a decision made by the user. Rather than a simple OK button, the confirm box includes both OK and Cancel buttons. Like the alert box, confirm is also a method of the window object.
This method returns a Boolean value, because there are two options. You can use confirmation boxes to ask the user a yes-or-no question, or to confirm an action. Here is an example:
var reply = confirm("OK to continue?")
reply is assigned a true value if the user chooses OK, and false if the user selects Cancel. The generic form of this function is confirm(message).
var name = prompt("Enter your name:", "anonymous")
var number = parseInt(prompt("Enter a number:", 0)) or var number = prompt("Enter a number:", 0) number = parseInt(number)
The generic form of this function is prompt(message[, inputDefault]). You can see that this function works by using the typeof operator for testing:
var number = prompt("Enter a number:", 0) alert(number + " is a " + typeof(number)) // "... is a string" number = parseInt(number) alert(number + " is a " + typeof(number)) // "... is a number" The input must be of a numeric type, of course (e.g., 99).