Home > Articles

JavaScript in Action

  • Print
  • + Share This
Not only are JavaScript inputs and outputs great for being able to write JavaScript code that will communicate two-way with the user, but exploring this area of JavaScript is also a great place to begin your journey to proficiency! In this excerpt from JavaScript 1.5 by Example, authors Adrian and Kathie Kingsley-Hughes take a look at JavaScript objects-c their methods and properties-and how to use inputs and outputs.
This article is derived from JavaScript 1.5 by Example .
This chapter is from the book

This chapter is from the book

Exploring JavaScript Inputs and Outputs

Not only are JavaScript inputs and outputs great for being able to write JavaScript code that will communicate two-way with the user, but exploring this area of JavaScript is also a great place to begin your journey to proficiency!

Computing is all about inputs and outputs. Data goes in and data comes out. Without inputs and outputs, nothing would happen and nothing would get done. A word processor doesn't begin to do anything until it receives input from the user (usually in the form of characters generated by keystrokes), and this then leads to an output (it is outputted onto the screen and subsequently to paper or electronically).

Here, we are going to use JavaScript to control inputs and outputs in the form of various types of message boxes (guaranteed, if you've been surfing the Web for more than a few minutes, you've seen these message boxes before!).

Three types of message boxes can be conjured up using JavaScript:

  • Alert—This is for outputting information.

  • Confirm—This outputs information and allows the user to input a choice in the form of a yes/no question.

  • Prompt—This outputs some information and enables the user to type in a response to the output.

NOTE

Why do the message boxes in Internet Explorer look so different from the message boxes in Netscape Navigator? This actually has nothing to do with JavaScript—they are different because the alert, confirm, and prompt windows are generated by the browser. These boxes are only triggered by JavaScript. Because of this, each browser adds its own uniqueness to the look.

alert(), confirm(), and prompt() are actually all methods of the browser's Window object.

Objects, Methods and Properties

One thing you've probably heard about JavaScript is that it is an object-oriented language. But what does this really mean? To understand this, you need to be familiar with three terms:

  • Objects

  • Methods

  • Properties

What follows is a brief look at the three. After you have used JavaScript for a little while, you'll find yourself using them a lot more, so we can leave the detailing of them until later.

Objects

Put simply, an object is a thing, anything. Just as things in the real world are objects (cars, dogs, dollar bills, and so on), things in the computer world are regarded as objects, too.

To JavaScript, its objects all live in the Web browser. These are things like the browser window itself, forms, and parts of forms such as buttons and text boxes. JavaScript also has its own group of intrinsic, or built-in, objects as well, which relate to things such as arrays, dates, and so on. At the moment, you don't need to think too much about these objects because they'll be covered later; for now you just need to get some of the necessary terminology in place.

But it is objects that make JavaScript object-oriented. JavaScript is organized around objects rather than actions; to put it another way, it's organized around data rather than the logic of the programming. Object-oriented programming takes the viewpoint that what you really care about are the objects you want manipulated and not the logic required to manipulate them. One of the benefits of this is that—because you are freed from the logic of the programming—not only is the process of programming (or scripting) easier, but there are also many ways to perform a particular operation.

Methods

Methods are things that objects can do. In the real world, objects have methods. Cars move, dogs bark, dollars buy, and so on. alert() is a method of the Window object, so the Window object can alert the user with a message box. Examples of other methods are that windows can be opened or closed and buttons clicked. The three methods here are open(), close(), and click().

Note the parentheses. Look out for these because they signal that methods are being used, as opposed to properties.

Properties

All objects have properties. Cars have wheels and dogs have fur. For JavaScript, things such as the browser have a name and version number.

TIP

It might help you to think of objects and properties as things and methods as actions.

Using the alert() Method

alert() is the easiest of the three methods to use. You can use it to display textual information to the user in a simple, concise way. When the user is finished reading the message, she simply must click OK to get rid of it.

First, open your template HTML page in your favorite text editor and save it with a new name in a convenient location on your hard drive:

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--Cloaking device on!
//Cloaking device off -->
</script>
</head>
<body>
</body>
</html>

NOTE

Remember to save it with the file extension HTM or HTML; otherwise, things won't work as planned.

CAUTION

Remember that JavaScript is a case-sensitive language.

To conjure the alert message box, type in the following:

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--Cloaking device on!
alert();
//Cloaking device off -->
</script>
</head>
<body>
</body>
</html>

NOTE

The semicolon at the end of the alert()method is called a line terminator and is required to make your JavaScript ECMA-compliant (although it will work properly without it).

Now, for the message that you want displayed. This is placed inside quote marks inside the parentheses:

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--Cloaking device on!
alert("An alert triggered by JavaScript!");
//Cloaking device off -->
</script>
</head>
<body>
</body>
</html>

Save the page, load it into the browser, and watch the message appear.

Making a second alert appear is just as simple as making the first one appear—just add alert() to the <script> block underneath the first and add your own message surrounded by quotes:

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript ">
<!--Cloaking device on!
alert("An alert triggered by JavaScript!");
alert("A second message appears!");
//Cloaking device off -->
</script>
</head>
<body>
</body>
</html>

Save the file in the text editor and refresh the browser. This time notice that two messages come up. But more importantly, notice how they are displayed separately, instead of one on top of the other. What this shows is that the JavaScript stopped between each and didn't just blindly run to the end of the script. It waited patiently until the user clicked OK on the first box before going on to display the second.

TIP

Remember to resave the file before viewing the changes you've made. You wouldn't believe the number of users who forget this step and think they have done something wrong!

Adding Comments to JavaScript

Professional JavaScripters strive to make it easy to reread their code when they come back to it (maybe many months later). One of the things they use to help them is the JavaScript comment tags, one of which you've already come across in the previous chapter—the single-line comment.

Single-Line Comments

Here is an example of single-line comment tags in action:

<head>
<title>A Simple Page</title>
<script language="JavaScript ">
<!--Cloaking device on!
//The first alert is below
alert("An alert triggered by JavaScript!");
//Here is the second alert
alert("A second message appears!");
//Cloaking device off -->
</script>
</head>
<body>
</body>
</html>

If you run this JavaScript, you won't notice any difference at all. That is because the comment tags have done their job and hidden the comments from the browser's view.

Multi-Line Comments

Sometimes you need to add multiple-line comments to your scripts, and although you could go round placing slashes (/) at the beginning of each line, it isn't really convenient. This is where the multi-line comment tags come into their own.

Multi-line comment tags consist of the open comment tag (*) and then the comments themselves followed by the closing comment tag (/).

Here is an example of a multi-line comment:

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript ">
<!--Cloaking device on!
/*
Below two alert()methods are used to
fire up two message boxes -note how the
second one fires after the OK button on the
first has been clicked
*/
alert("An alert triggered by JavaScript!");
alert("A second message appears!");
//Cloaking device off -->
</script>
</head>
<body>
</body>
</html>

Adding meaningful comments to a script is something that only comes with practice. At first, it might seem tedious or even unnecessary, but not only does it make your JavaScripts easier to understand, in the early stages it helps you get a clearer idea of how your scripts work. Take every opportunity possible to comment your scripts!

Using the confirm() Method

The confirm() method works similarly to alert(), but this box is used to give the user a choice between OK and Cancel. You can make it appear in pretty much the same way, with the only difference being that instead of using the alert() method, you use the confirm() method.

So again, following the same routine as for the alert() method, you begin by adding the confirm()method to the script block as follows:

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript ">
<!--Cloaking device on!
confirm();
//Cloaking device off -->
</script>
</head>
<body>
</body>
</html>

And again, the message you want to appear in the box is typed inside the parentheses, contained in quotes:

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript ">
<!--Cloaking device on!
confirm("Which one will you choose?");
//Cloaking device off -->
</script>
</head>
<body>
</body>
</html>

Save the file (again under a different name from the template and remembering to use the HTM or HTML file extension) and load it into the browser.

Now notice the buttons on the box: OK and Cancel. However, at the moment nothing happens when you click them except that the box disappears and the JavaScript continues to run again. Before you can use the buttons on the confirm box, you will need a few more JavaScript skills, after which you will revisit the confirm() method.

Using the prompt() Method

The prompt() method is a little different from the other two you have looked at in the course of this chapter. This is the only one that either allows the user to type in his own response to the question, instead of the script just processing information it already has (as with the alert() method), or allows the user to choose OK or Cancel (available using the confirm() method).

You add the prompt() method in much the same way as the other two. To begin with, add the prompt() method to the <script> block:

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript ">
<!--Cloaking device on!
prompt();
//Cloaking device off -->
</script>
</head>
<body>
</body>
</html>

The prompt() method now starts to differ from the other two methods because two pieces of text must be added within the parentheses. The first is the message you want to appear.

This is done in exactly the same way as before. Again, the text goes inside the parentheses and inside quotes:

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript ">
<!--Cloaking device on!
prompt("What is your name?");
//Cloaking device off -->
</script>
</head>
<body>
</body>
</html>

If you save this page and view it in the browser, notice that the prompt appears, asking the user for his name.

  • + Share This
  • 🔖 Save To Your Account