|
|
Review of JavaScript Basics
A fast but fairly comprehensive introduction to JavaScript can be found in the
last part of the
Course Notes
for Hypermedia: An Introduction, revised last summer.
We'll go beyond what was covered in that course, but if you're hazy about JavaScript,
have a look at the notes.
Meanwhile here's a quick digest of key points:
- JavaScript is not Java. Aside from the deceptive name and some
similarities of syntax, there's no connection between Java (created by Sun Microsystems)
and JavaScript (created by Netscape), even though Sun and Netscape are now "allied" companies.
JavaScript is a scripting language, entirely dependent on the browser that implements it.
Java is a programming language in which one could write, among other things, a browser.
- Where scripts live. Scripts can occur in-line, as directly-executable
commands written into a
<SCRIPT></SCRIPT> container within the body of a document; internally, as instructions within a container outside the body, usually in the head portion of the document;
or externally, on special text-only pages with a ".js" extension. In the last
case, the external script is invoked with an empty <SCRIPT></SCRIPT> container whose initial tag includes an SRC attribute. (See the Intro Notes for
examples and details.)
- How scripts are used. Scripts can be executed directly as the page is laid out
(e.g., the footer script at the bottom of this page) or they may be invoked in one of two
ways. Most scripts are invoked through handlers, special attributes of HTML tags
such as onLoad in the initial <BODY> tag. Scripts may also be invoked
from within other scripts--even within themselves, in the case of recursivity.
- Functions. In order for a script to be invoked by any of the methods just
mentioned, it must be written as a function. This is the general form of a function:
function someName()
{
[JavaScript statements here]
}
the script is invoked with its name followed by a set of parentheses, e.g.:
onLoad="colorize(3,'#990000')"
The parentheses may contain one or more parameters, values to be plugged into variables
used by the function. In the example above, the "3" might tell the function which paragraph
to colorize and the string "#990000" might indicate what color to apply. The corresponding
function definition might look like this (the parameter names are arbitrary):
function colorize(whichPar, whatColor)
{
[JavaScript statements here]
}
- How widely can JavaScript be used? All major PC
browsers recognize the <SCRIPT></SCRIPT> container and
most will perform a wide range of JavaScript instructions. However, there are some
notorious gaps in coverage. For more discussion of this sad subject,
see this week's notes on
Platform Detection.
|