JavaScripting for Interactive Educational Webs

[Intro] [Overview] [Basics] [More] [Forms] [If] [Queries] [Time] [Random] [Loops] [Misc.] [References] [HTML]


Getting Started with JavaScript

Abstract: A short introduction to JavaScript and the methods of inserting JavaScript in HTML programs. Covers the basic forms of input and output, variables, and expressions.


Introduction

JavaScript is a programming language for making interactive pages which looks a lot like C and supports standard programming concepts like variables, conditionals, loops, functions, and objects. The following discussion assumes that you understand most of these basic concepts.


Simple Input and Output

Since we'll need some commands to get started, we'll begin with input and output. For now, we'll simply describe the commands. In the next section, we'll see how to incorporate them into a page.

One way to provide information to the user is with the alert(text) function. This function brings up an alert box which displays the argument to the function. Many people find the alert box a simple way of providing a greeting without "muddying" the underlying HTML.

A similar way to obtain information from the user is with the prompt(request,default) function. This function brings up a dialog box in which the user can type a response. In most cases, you will want to include a default setting. The function returns the string the user entered.


JavaScript Programs

Although you will generally incorporate JavaScript in HTML pages, you can also save them as xxx.js files and load them into navigator. This is often useful for quick testing of concepts.

However, there are many advantages to embedding your JavaScript in HTML code. Among others, you can load JavaScript libraries within HTML pages, but not within other JavaScript files.


JavaScript in HTML Files

Their are a number of ways to insert JavaScript code into HTML documents. We will begin with two of the simpler ones.

For JavaScript code that is to be executed directly, one uses the <SCRIPT> tag. The tag has two optional parameters,

For example, a simple JavaScript program might include

<script language="javascript">
alert("Welcome to my page.");
</script>

The src parameter is particularly useful, as it lets you reuse your JavaScript code. I strongly encourage you to develop general functions, and then incorporate them into your program using the <script src="..."> tag.

We can extend that simple JavaScript program to incorporate a library file as follows. Note that we need two sets of <SCRIPT> tags: one set to load the library and another to call a function from that library.

<script language="javascript" src="greet.js"> </script> <script language="javascript"> greet("Sam"); </script>


Objects and Methods

JavaScript is at least somewhat object-based. That is, it presents many things to you as objects, and lets you manipulate them as such. For example, you can refer to the current window as window or the current document as document.

The pieces of information associated with an object are called the properties of that object. A property may be a number, some text, or even another object). To refer to a property of an object, you use the name of the object, a period, and the name of the propety. For example, you can obtain the title of a document as document.title.

Objects also have associated functions, which are normally called methods. These are the things that the object knows how to do. For example, you could tell the document to close itself with document.close().

For certain types of predefined objects, such as windows and arrays, you create a new object using the new command.


Exercise: Your first JavaScript program(s).


[Intro] [Overview] [Basics] [More] [Forms] [If] [Queries] [Time] [Random] [Loops] [Misc.] [References] [HTML]

This page written by Samuel A. Rebelsky.

This page generated on 42 by SamR's Site Suite.