Interactive Boxes



index
Disabled back button Next Section
printable version

Section 1: Overview

JavaScript provides three types of interactive boxes, or popup dialog boxes: alerts, confirmations, and prompts.

Section 2: alert

The JavaScript alert is the simplest and the most often used of the three available JavaScript pop-up dialog boxes.

The syntax for alert is

alert (<msg>);

or

window.alert(<msg>);

where <msg> is either a string literal or a string variable. For example,

alert("This is the message to be displayed in the pop-up window!");

or

var msg = "The message can also be in a variable!";
alert(msg);

The message inside the parentheses is shown in the alert.

You can also combine variable values and text strings by using the + sign. For example:

var course = "INFO 2220";
alert("My most challenging course is " + course);


alert() is a method of the window object and cannot interpret HTML tags, so we can't write text on multiple lines in an alert box using the <br/> tag.

An escape character consists of a backslash (\) symbol and an alphabetical character.

alert("JavaScript\nis\na\nclient-side\nprogramming\nlanguage");

You'll notice that new lines have been introduced at each occurrence of the \n escape character.

Notes:

Section 3: confirm

The JavaScript confirm is similar to an alert, but has both an 'OK' button and a 'Cancel' button.

To enable you to test a user's response, a confirm returns a Boolean true if the user clicks 'OK' or hits 'Enter', and it returns a Boolean false if the user clicks 'Cancel' or hits 'Escape'.

The syntax for confirm is

var returnValue = confirm (<msg>);

or

var returnValue = window.confirm (<msg>);

where <msg> is either a string literal or a string variable. For example,

var showMessage = confirm ("Is it ok to display a message now?");

or

var msg = "The message can also be in a variable!";
var showMessage = confirm (msg);

You then test to see if the return value is true or false

if (returnValue )
{
   // 'OK' script goes here
}
else
{
   // 'Cancel' script goes here
}

function confirmDemo()
{
   // confirm requires one argument
   var message = "Please confirm that it is OK to proceed";

   // confirm is Boolean, which means that it returns
   // true if 'OK' is clicked, otherwise returns false
   var OkClicked = confirm(message);

   // Test to see if true/false is returned
   if (OkClicked) // Or if (OkClicked == true)
   {
      // place the 'OK' script here
      alert("The user clicked OK.");
   }
   else
   {
      // place the 'Cancel' script here
      alert("The user clicked Cancel.");
   }
}

Section 4: prompt

The JavaScript prompt is the most complex of the three JavaScript popup dialog boxes in that it accepts two arguments and returns two different classes of response.

As pointed out, the prompt returns two different classes of response:

When the browser encounters a prompt statement in a script, the prompt popup box is displayed on the screen and all other operations are suspended until the user reacts to the prompt.

The syntax for prompt is

var returnValue = prompt (<msg>, <default>);

or

var returnValue = window.prompt (<msg>, <default>);

where <msg> and <default> are either string literals or string variables. For example,

var courseNum = prompt ("Enter the course number of your favorite course: ", "INFO 2220");

or

var msg = "Enter the course number of your favorite course: ";
var default = "INFO 2220";
var courseNum = prompt (msg, default);

You should next check to see if the user failed to accept even the default. In some browsers you check to see if it is null, and in others you check to see if it is an empty string.

if ((courseNum != null) && (courseNum != ""))
{
   alert("I hope you are enjoying " + courseNum + "!");
}
else
{
 alert("You didn't specify a course! Do you hate them all?");
}

function promptDemo()
{
   var favPage=prompt("What is your favorite web site?", "http://");

   if ((favPage != null) && (favPage != ""))
   {
      window.location=favPage;
   }
   else
   {
      alert("You didn't specify a favorite page, so I'm not going anywhere!");
   }
}

Section 5: Resources