According to Security Space (April, 2007),
about 60% of Web sites use JavaScript.
There are some differences between versions of JavaScript;
for example,
Gecko, the engine used in more recent versions of Netscape Navigator,
does not support the layer functions
that were available in some earlier versions of Navigator.
In addition to a version of JavaScript
(sometimes referred to as JScript),
Microsoft Internet Explorer also supports another scripting language
called VBScript,
which uses a subset of Visual Basic.
Simple JavaScript
A passage of script in an HTML file
is introduced with the script tag
and terminated with the /script tag.
In addition,
the statements within the script are normally commented out
with <!-- and -->.
Thus, the general form of a passage of JavaScript/JScript code
in an HTML file is
Within JavaScript,
// can be used to comment out the rest of a line,
as in the second-last line above.
The noscript and /noscript tags
can be used mark text that will be included
only if the required scripting language or languages
are not enabled;
for example,
<noscript>your browser does not have JavaScript1.1 enabled.
</noscript>
A simple way to use JavaScript
is just to have it insert bits of content
into a page when the page is rendered.
This can be done with the write
or writeln method
of the document object.
The parameters of these methods
can be any series of expressions or properties
whose values you want displayed.
For example,
the following code is supposed to tell the user
what browser type they are using:
<script language="JavaScript1.1">
<!-- JavaScript begins...
document.writeln('you are using ', navigator.appName, '.')
// JavaScript ends -->
</script>
That is how this page can tell you that
Browsers can lie about their identity, however,
and often do.
Literal strings can be enclosed
either in single quotes (as above)
or in double quotes.
The dot notation is used for properties of objects
(as in the example above).
It is also employed for methods
(functions associated with a particular type of object).
Variables
For scripting of any complexity,
you need to make use of variables.
Variables are fairly simple objects
that are assigned values with the = operator.
For example,
the following code creates a new Date object
for today's date,
assigns it to the variable today,
and then uses the Date object's getDay() method
to display the day of the week
as a number
(where Sunday is 0, Monday is 1, and so on.)
An array object can be used to contain a list,
an individual item of which can be referenced
by appending a numeric expression in square brackets to the
array name.
For example,
the following code creates a new array
containing the names of the days of the week
and uses it to display the name of the day of the week today.
Browsers do not provide much help with debugging problem JavaScript code.
With the exception of Internet Explorer, however,
they do generally provide optional consoles
that display scripting errors.
In Netscape,
select "JavaScript Console"
from the "Web Development" submenu
of the "Tools" menu;
in Firefox,
select "JavaScript Console"
directly from the "Tools" menu;
in Opera,
select "JavaScript Console"
from the "Advanced..." submenu
of the "Tools" menu.
In Internet Explorer,
the procedure to see errors is a little more roundabout
and the results a bit more limited:
load a page that causes an error,
double click on the exclamation-triangle symbol
at the bottom left (in the status bar),
check "Always display this message
when a page contains errors",
click on "Show Details >>",
click on "OK".
For More Information
There are many references
that you can consult for more information about JavaScript,
though none appears to be truly comprehensive.
A selection follows.
JavaScript Kit. 2007.
JavaScript Kit- Comprehensive JavaScript, DHTML, CSS tutorials and over
400+ free JavaScripts!http://www.wsabstract.com/.
(Sample scripts and tutorials.)
Jupitermedia. 2007.
JavaScript Source:
Free Javascripts, Tutorials, Example Code, Reference, Resources,
and Help.
http://javascript.internet.com/.
(More than 2000 samples of JavaScript code.)
For print books,
try the Library of Congress subject heading
JavaScript (Computer program language)
or DHTML (Document markup language)
in the library catalogue.