<Lesson 2> [Contents] <Lesson 4>
3.1 JavaScript Dialog
A dialog box that pops up on a web page will enhance the interactivity of the web page. It is normally used in advertising. A JavaScript dialog carries a message or a question that required some form of actions from the user, either clicking a button or pressing a key on the keyboard.In JavaScript, the syntax to launch a dialog is very simple, you just need to use the alert method. The structure is:
<html> <head> <script language="JavaScript"> windows.alert("Welcome to JavaScript Tutorial"); </script> </head> </html>
Adding this Script to a web page allows the user to see a popup dialog box that displays the message “welcome to JavaScript Tutorial” as shown in the diagram below:
Click on Example 3.1 to view the dialog box.
You can customize the format of the output using a backslash \ and “n”. Lets change the line windows.alert(“Welcome to JavaScript Tutorial”) to
window.alert”welcome to\nJavaScript\nTutorial”). The output will be displayed in three lines now, as shown below:
If you put the two scripts together, you will see two dialog boxes appear successively, after you click the first one, the second one will appear.