ptg16476052
472 LESSON 17: Introducing JavaScript
Why Would You Want to Use JavaScript?
JavaScript changes the browser from an application for displaying documents to a plat-
form for writing applications. It’s useful because it enables the developer to manipulate
the contents of a web page after it has loaded, making it possible to provide users with
instant feedback when they make a change in a form, loading resources on demand for
performance reasons, or building full-blown user interfaces that resemble desktop appli-
cations.
JavaScript is useful because it’s deeply integrated with the browser. This integration
allows programmers to manipulate various aspects of the browser behavior, as well as
objects included on the page. JavaScript uses what’s referred to as an event-driven model
of execution. When you embed JavaScript code in a web page, it isn’t run until the event
it’s associated with is triggered.
The types of events that can call JavaScript include loading the page, leaving the page,
interacting with a form element in some way, clicking a link, or even just scrolling up or
down. Plenty of other events are available, too. Often these events are utilized in what
most users would consider to be annoying ways. For example, many sites open an addi-
tional window containing an advertisement when you navigate to one of their pages. This
is accomplished using JavaScript and the page load event. Other sites open additional
windows when you leave them; this is also accomplished using JavaScript triggered by
the page unload event. Less annoying applications include validating forms before they
are submitted or displaying extra information on a page when a user clicks a link without
requiring a complete page refresh.
This introduction will by necessity be briskly paced. There are
many books written about JavaScript alone. The goal of these les-
sons is to introduce you to JavaScript, enable you to get started
accomplishing tasks, and hopefully kindle your interest to dig into
the language more deeply.
NOTE
JavaScript enables you to manipulate web pages without sending a request back to the
server or to send a request to the server to retrieve information without leaving the page
that the user is on. Using these capabilities, you can change the contents of a page,
change the style of elements on a page, validate user input before a user submits a form,
and modify the behavior of the browser—all by using scripts embedded within your web
pages. Let’s look at some of the advantages of using JavaScript to enhance your web
pages.