Previous
The Basics 
Next
Variables 
ABC of JavaScript : An Interactive JavaScript Tutorial
Dialogs

Dialogs

The elements in a program that detaches itself from the main window can be called dialogs. There are three main dialogs in JavaScript

alert

Syntax:
alert("STRING")

We had seen this dialog before. This will display the string given as argument in a Message box. You can display variables by adding it the string...

word = "ECNALUBMA (ek na lub' ma)";
meaning = "A rescue vehicle which can only be seen in the rear view mirror.";
alert("The meaning of '"+word+"' is "+meaning);

Here the first part of the string('The meaning of ') is concatenated - or added - to the next part(word or 'ECNALUBMA (ek na lub' ma)') and that is added to the next part and so on. So the whole string is shown together.

One can also use special characters in stings - '\n' means new line, '\t' means a tab character etc.

As always you are welcome to try out what you learned using the T-Box.




confirm

Syntax:
VARIABLE = confirm("STRING")

The alert dialog has only one response - 'OK'. The confirm dialog has two possible responses. This is useful in situations where the user should make a yes or no decision. This function will return the user decision. An example

var answer = confirm("Unable to FORMAT A:\nHave a go at C:?");
if(answer==1) alert("C: Formatted. Windows not found:\n(C)heer (P)arty (D)ance.");
else alert("C: not formatted. Out of disk space. Delete Windows?\n(Y)es (H)ell yes!");

The confirm function will return 1 if the user clicked 'OK' or will return 0 if the user clicked 'Cancel'. And don't worry - nothing was formatted.




prompt

Syntax:
VARIABLE = prompt("STRING"[,"DEFAULT ANSWER"])

This dialog will ask user a question and wait for the answer. Once the user inputs a answer, the function will return that value. You can also specify a default answer - for those users how are too lazy to type.

var answer = prompt("What's the best feature of Windows?","The UnInstaller!");
alert("So you think that "+answer+" is the best feature of Windows");

If the user clicks cancel, null value will be returned.

Try out what you have learned.

Previous
The Basics 
Next
Variables 
blog comments powered by Disqus