JavaScripting for Interactive Educational Webs

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


More Simple JavaScript

Abstract: Further information to help those getting started with JavaScript.


Variables

As with many programming languages, JavaScript allows you to define and use variables -- named containers of information. Unlike many languages, JavaScript supports only untyped variables. You can use the same variable to store some text, a number, or an object.

You assign values to variables by writing the variable name, an equals sign, and the expression you want to assign to the variable. For example, alpha=2 assigns the value 2 to the variable alpha.

You declare a new variable with var varname If the declaration is within a function, the variable is local to that function. If the declaration is outside of a function, the variable is global (accessible from any function).

Surprisingly, JavaScript does not require you to declare variables. However, I strongly encourage you to declare your variables and add a note as to how you're using them.


Comments

As you develop longer and longer programs, you should insert notes to yourself (or to others) to help explain the program. These are typically called comments. In JavaScript, comments begin with two slashes, //, and end at the end of a line.


Expressions

JavaScript permits normal mathematical expressions, such as 2*10+3. It also permits you to use variables within expressions.

JavaScript also provides some basic string operations. In particular, + is used to concatenate strings. If you happened to have the user's name stored in the variable userName, you could create a greeting with

var greeting // A greeting to the user
greeting = "Hello " + userName + ".  Welcome to this page."


Strings

As you've already noted, strings in JavaScript are usually surrounded by double quotation marks. You can also surround them by single quotation marks (which may be useful when you need quotation marks within your strings).

JavaScript treats strings as objects, and provides a number of methods and properties to help you use and manipulate your strings. Here are some of the more useful methods and properties.

In JavaScript, the initial character in a string is character 0, rather than character 1. In addition, the length property seems to give the index of the last character, rather than the length of the string.

There are many uses for these string functions. For example, if I was given the full path to a file, such as /usr/rebelsky/programs/javascript/sample.js, and I wanted to extract just the file name, I might use something like the following

// We'll assume that variable fullPath stores the full
// path to the file
var lastSlash // the position of the last slash in the path
var fileName  // the name of the file
var directory // The directory
lastSlash = fullPath.lastIndexOf("/")
fileName = fullPath.substring(lastSlash+1,fullPath.length)
directory = fullPath.substring(0,lastSlash)


Functions

JavaScript allows you to define your own functions. By writing functions, you can better test and reuse your code. The normal structure of a function is

function function-name ( parameters )
{
  commands
} // function-name

The left brace indicates the beginning of the function body, and the right brace indicates the end of the function body.

You use return expression to return a value from a function.

As an example, I might turn the previous bit of code into a function to extract just the file name from a path with the following. Note that I've added a number of comments for the next time I look

// Function:
//   extractFileName(full-path)
// Parameters:
//   The full path to a file, in Unix or Web-style
//   (directories are separated by slashes)
// Returns:
//   The name of the file, without the full directory
//   information.
// Caution:
//   May not work correctly if no directories precede
//   the file name (this will be fixed in a later version).
// Example:
//   extractFileName("/usr/lib/alpha.pm") returns "alpha.pm"
function extractFileName(fullPath)
{
  var lastSlash // the position of the last slash in the path
  var fileName  // the name of the file
  lastSlash = fullPath.lastIndexOf("/")
  fileName = fullPath.substring(lastSlash+1,fullPath.length)
  return fileName
} // extractFileName


Inserting text and HTML

Up to this point, we've only been interacting with the user by putting up dialogue boxes. Clearly, we'd like to do more. For example, we might want to insert text or HTML into a document. JavaScript allows us to do this with the write method of the document object. That is, you use document.write(text) to insert text. The text is inserted at the point that the JavaScript code is executed. You use document.writeln(text) to write the text an add a carriage return tot he end of the line.

For example, we might create a page that greets a user by name with

<script language="javascript">
var userName
userName = prompt("Please enter your name", "")
document.write("

Hello " + userName + " welcome to my page.

") </script>

Note that I've included spaces in the strings so that the user's name does not run into the surrounding text. Also note that I've included HTML tags within the text I've written.

There is a drawback to the document.write() method: it only works when the document is loaded (or reloaded). Hence, you cannot later insert text into your document (unless you reload it).


Exercise: Your first custom HTML pages.


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

This page written by Samuel A. Rebelsky.

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