This book is loosely divided into two parts. The first part is primarily
concerned with the different components in JavaScript, from basic mathematical
expressions to the use of cookies to store persistent state between pages. Each
chapter in this section explores in detail the possibilities surrounding the
components, showing by way of practical, everyday, reusable example applications
how to make use of the components. Chapters in this section include:
Chapter 2-Maths and Numbers
This chapter describes two calculators written in JavaScript, a standard
calculator, and a scientific calculator. Shows how to use a random number
generator to display random quotes. Includes several number utility functions
that are reused many times throughout the remainder of the book.
Chapter 3-Strings and Regular Expressions
This chapter shows how to find and replace strings within other strings,
along with a practical demonstration with a copyright boilerplate. It also shows
how to create scrolling text links, how to manipulate a contacts list, and how
to manipulate strings to apply Gourad shading. This chapter describes how to
parse text strings, and finally how to use Regular Expressions to validate form
data input.
Chapter 4-Arrays and Objects
This chapter shows how to hold data as objects and how to hold objects within
arrays, to show how to visit one of many web sites in an automated tour. It also
explores how to search data held in a database, along with a multiple-choice
test, and finally how to sort an array of objects.
Chapter 5-Location and Links
This chapter describes how to interact with the current location to provide
automatic redirection to a new location, shows how to automate previous and next
buttons between different pages, and automate links between portions of the same
page, describes how JavaScript can be used to manage a ring of related
web-sites. It shows how to extract the browsers preferred language and loads an
appropriately written web page. There's also information on how to password
protect resources and verify the entered password, and finally how to write a
JavaScript link crawler to crawl through all the links in a connected set of
pages.
Chapter 6-Windows and Frames
This chapter shows how to target the entries in a select form list to various
windows in a frame set. It describes how to read the entries in a directory
listing and describes a tool to generate your own popup windows. The use of
popup windows in a dictionary lookup is also illustrated, along with the use of
frames to hold data between page views to provide a family tree application. The
chapter addresses how popup windows can be used to debug your own applications,
followed by a popup date selector application, and finally code to stop other
sites framing your own pages, while at the same time provide a means to ensure
your pages are correctly framed within their own frame set.
Chapter 7-Date and Time
This chapter explores how to work with and manipulate the current date and
time. It shows how to extract the day of the week for any date, how to validate
an input date, and display a different page based on the time of day. It
describes how to show a yearly calendar, how to count forwards and backwards to
different dates, how to calculate an age in years, months and days, and shows
how to work with the zodiac and Chinese calendars. The chapter includes
applications to show events, religious celebrations and birthdays associated
with a date, and finally shows how to work out the current time anywhere around
the world.
Chapter 8-Form
This chapter first shows how HTML forms and JavaScript can interact and
includes applications to show how to send email forms, how to interact with drop
down menus, and how to pass data from one form to another. It covers how to
provide a generic and dynamic thank you page following a form submission and
includes applications to pass data from one form to another by way of cookies
and emails. It describes how to disable form fields in HTML 4, and includes a
technique to control form data entry. It also covers how to filter data input,
and how to generate a new form component to spin date values up and down through
a list, and finally shows how to provide a generic sales order form to calculate
the sub-totals and total along with any necessary taxes.
Chapter 9-Images
This chapter covers the ubiquitous highlighting of images, and a fast
implementation of image rollovers, with a variation that fades the images in and
out. It demonstrates how to use image maps with applications that show an image
slide show, and bar chart generation. It also shows how to count the number of
images downloaded, popup windows to hold images, including an image counter,
random banner adverts, and an image toolbar.
Chapter 10-Cookies
This chapter addresses general-purpose cookies functions for saving,
retrieving and deleting cookies, and shows how to combine cookies with HTML to
mark items as new. Intelligent cookies that avoid continual writing of cookies
when disabled within the browser are also covered. The chapter's cookie tracker
application demonstrates how cookies can be used to track a visitor's movement
around a web site, and the personal calendar application shows how cookies can
be used to store personalization information.
The second part of the book builds on the first part to generate far more
complex applications, and at the same time build a cross-browser code library to
support Dynamic HTML techniques in both Netscape Navigator (versions 4 and 6)
and Internet Explorer (versions 4 and 5). Chapters in this section include:
Chapter 11-Dynamic HTML
This chapter describes the Document Object Model, including those available
in Internet Explorer 5 and Netscape Navigator 6. Builds a crossbrowser
compatible library for manipulating the DOM. It demonstrates the use of a
cross-browser library to collapse content, dynamically change the cascading
style sheet used to display the page, display tool tips that float above the
document, handle multi-dialog forms, and toolbar menus with drop-down content.
The library is also used to automatically generate a table of contents from the
heading tags within a document, to slide tabbed content from the sides of the
document, along with a hierarchical menu structure that can be opened and closed
to show different leafs within a tree structure. The final application
demonstrates a dynamic advert that interacts with the user.
Chapter 12-Applications
This chapter shows fully fledged applications, including two versions of a
shopping cart application, one using simple hypertext links and form buttons to
interact with the user, and the second using drag n' drop techniques. The
myPage.com application shows how the user can personalize their view of your
page. The computer builder/selector demonstrates how different complex views of
several products can be generated from a simple database of products and
components. The book catalog applications show how JavaScript can interact with
an XML document the DOM and style sheets all within one application.
Chapter 13-Utilities
Various uses of JavaScript are covered in this chapter. The color picker
allows you, the author, to experiment with mixing background and foreground
colors. The onscreen keyboard shows how users can interact with the page even
when a keyboard isn't present. The automated scrolling application demonstrates
how the page can be scrolled automatically without user intervention. The
homepage script can be used to select the current page as the users default
homepage. The final application shows how to detect for support of plugins and
ActiveX components, both of which extend the basic browser beyond what it is
capable of.
Chapter 14-Fun Stuff
The final chapter covers the more fun aspects of JavaScript, including three
JavaScript games-from the simple icon match game to the more interesting
JavaScript versions of Tetris and Columns. The amusing mouse trail application
shows how interesting DHTML techniques can be used to follow the mouse pointer
around the screen, while the final application of the book, the personality
test, can be used to rate the user's personality, or lack thereof.
All of the examples in this book are exactly that: examples. Examples that
can be adapted to your needs, or perhaps provide inspiration of what JavaScript
is capable o£ Rather than a dry, and perhaps dull reference book on
JavaScript, this book instead illustrates JavaScript applications, nearly 100 in
total, that can be used as they stand or built on to provide even more complex
and interesting programs.
Accompanying CD-ROM
All the example code, including the applications, is included on the
accompanying CD-ROM, as denoted by the CD-ROM icon. There should be no need to
type in the code by hand. If, however, you decide to do this you should first
check the code on the CD-ROM before reporting any problems with your code.
If you do have problems with the code on the CD-ROM then you can report these
along with any errors or omissions in the book, or any suggestions for
improvements in future editions of the book, to Martin Webb at martin@irt.org.
Any updates or errata found after the book has gone to print will be detailed
online at the supporting web page at http://www.irt.org/instant/.
As the book progresses less and less of the code is presented in the book.
This is to first highlight the most important aspects of the code, and secondly
to reduce the size of the overall book. However, the accompanying CD-ROM
includes all the code for all the applications in full. The first few chapters
of the book do however include all the necessary JavaScript code and HTML
markup.