Chapter 19: JavaScript: Adding Dynamic Content to Web Pages
Note: this page applies to the now-obsolete first edition of the book. For
the second edition, please see
http://www.corewebprogramming.com.
This chapter of
Core Web Programming (pages 1040-1145) introduces JavaScript,
summarizes the syntax, discusses the major applications, and shows how
to make JavaScript portable. Detailed coverage of the JavaScript 1.0-1.2
API is reserved for the
following chapter.
Contents
Reminder: On Windows 95/NT and Unix, click on the links with
the right mouse button to save the referenced Java file to a new location.
On any platform, you can also click on the links with the SHIFT key
held down. On Netscape, this lets you save the source for the
referenced file to a new location. In Internet Explorer, this puts the
referenced file in a separate browser window (without frames), where
you can then save it.
-
JavaScriptTemplate.html. Shows the basic structure of how
you insert SCRIPT elements into the BODY part of an HTML document.
-
FirstScript.html. A very simple page where JavaScript builds
part of the HTML.
-
ShowInfo.html.
A page that shows information about
the page and browser. Illustrates that JavaScript can generate
different HTML in different situations.
-
NonJavaScriptVersion.html. One alternative for building
sites that support both JavaScript and non-JavaScript browsers.
In this approach, the original page is written without
JavaScript, but has a single JavaScript entry that forwards
JavaScript-capable browsers to a new page.
-
JavaScriptVersion.html. This is the page people with
JavaScript browsers end at when visiting
NonJavaScriptVersion.html.
-
DontClick.html. A simple illustration that JavaScript can be
used to monitor user events, not just to dynamically build HTML.
-
ForIn.html. An illustration of the use of the
for(field in object) construct.
-
DynamicTable.html. An example of how JavaScript can be used
to selectively make use of browser extensions. Here, the
table headings are white text on black background in Netscape
and IE 3 or later, but not in Netscape 2, which understands the
"use white text" part but not the "use a black background" part,
and would otherwise have unreadable white-on-white headings.
-
Strawberries.html. Illustrates that in JavaScript 1.2
(Netscape 4), you can look up the browser window size and adjust
image and font sizes appropriately.
-
Plugins.html. Shows how JavaScript 1.1 and 1.2 can access
information about your browser's available plugins.
-
ImageButton.html. Using JavaScript to animate images, you
can create an image button that, when clicked, takes you to
a specific URL and is briefly displayed in inverse video
when clicked.
-
HighPeaks.html. Illustrates the use of JavaScript to
"highlight" clickable images when they are under the mouse.
The actual JavaScript code is in the
HighPeaksNavBar.html frame cell.
-
HighPeaksNavBar.html. Used in the HighPeaks example, this
frame cell has the actual JavaScript image-manipulation code.
It works on all JavaScript browsers that support images, but
does not cause errors on those that don't.
-
HighPeaksIntro.html. This frame cell shows the initial "main
text" in the HighPeaks example.
-
Camps.html. This illustrates the use of JavaScript to
manipulate HTML "layers". Here, the layers appear/disappear and
move around on the screen based on user actions.
-
CheckText.html. An example of using JavaScript to validate
individual CGI form fields one at a time. Note that there is
not really a CGI program at the other end that shows available
training courses; the field validation via JavaScript is the
point here.
-
Numbers.html. An illustration of how to validate the format of
numbers in different JavaScript versions.
-
CheckSeveral.html. An example of using JavaScript to validate
multiple CGI form fields in one fell swoop when the form is
submitted. Note that there is
not really a CGI program at the other end that registers you for
camp; the field validation via JavaScript is the
point here.
-
Widgets.html. This illustrates how JavaScript can manipulate
cookies: reading and parsing existing values, and asking you
to store them persistently.
-
ShowURL.html. Shows how JavaScript can manipulate
frames. Here, a URL entered in one cell is displayed in another.
This file is the place to start for trying it out, but
GetURL.html is the place to go to see the actual JavaScript code.
-
GetURL.html. Code used in
ShowURL.html.
-
DisplayURL.html. Initial frame cell contents for
ShowURL.html.
-
Summary of how to call Java from JavaScript. On-line excerpt
from section 19.9.
-
CallJava.html. This illustrates how JavaScript with
LiveConnect can call builtin Java methods directly. Works only in
Netscape 3 and 4 and the Windows 95/NT versions of Internet
Explorer 4.
-
Wonder-Widget.html. This illustrates how JavaScript with
LiveConnect can call methods in applets that are embedded in the
current page. In this particular case, the applet provides
the client hostname (something JavaScript can't check directly),
and different resumes are provided depending on whether the
reader is inside the company or outside.
Works only in Netscape 3 and 4 and the Windows 95/NT versions of
Internet Explorer 4.
-
GetHost.java. This is the applet used by
Wonder-Widget.html.
-
ResumeLoyal.html. This is the resume given to readers of
Wonder-Widget.html who are reading from company computers.
-
ResumeReal.html. This is the resume given to external readers of
Wonder-Widget.html.
-
MoldSimulation.html. Shows how JavaScript can control
applets. Works only in Netscape 3 and 4 and the Windows 95/NT
versions of Internet Explorer 4.
-
RandomCircles.java. The applet embedded in
MoldSimulation.html.
-
Summary of how to call JavaScript from Java. On-line excerpt
from section 19.10.
-
ShowHTML.java. A Netscape-3 specific way of building
HTML on-the-fly and displaying it, all from a Java applet that
does not use the JSObject class.
-
TestHTML.java. Driver/test applet for the ShowHTML class.
-
TestHTML.html. On-line test of the TestHTML applet. This
will only work if you try it from Netscape 3.x.
-
TestHTMLTop.html. Frame cell used in TestHTML example.
-
TestHTMLBottom.html. Frame cell used in TestHTML example.
-
MatchColor.html. Shows how Java can call JavaScript to
get information about the page the applet is embedded in.
Here, it looks up the color of the surrounding page and
matches it. Works only in Netscape 3 and 4 and the Windows 95/NT
versions of Internet Explorer 4.
-
MatchColor.java. Applet used by
MatchColor.html.
-
Everest.html. A Java applet that controls HTML form values.
Works only in Netscape 3 and 4 and the Windows 95/NT
versions of Internet Explorer 4. Note that the form submission
button is not really attached to anything; the Java-JavaScript
interaction is the point here.
-
Everest.java. Code used for applet in
Everest.html. Requires the
LabeledCostSlider,
CostSlider,
Slider, and
ScrollbarPanel classes.
-
LabeledCostSlider.java. A numeric slider class with attached
label. Used in the Everest example.
-
CostSlider.java. A slider class that lets you read numeric
values. Used in the LabeledCostSlider class.
-
Slider.java. A slider class: a combination of Scrollbar and
TextField. Used in the CostSlider class.
-
ScrollbarPanel.java. A Panel with adjustable top and bottom
insets, used by the Slider class to change the thickness of
the Slider.