finished lo ading, a call to write() will reopen the document, erasing all its contents.
In the old da ys of JavaScript, the write() me thod used to be the only way of generat-
ing the document’s content d ynamically, but in modern web programming this method
is seldom used.
Events
There are few events that browsers fire directly at Docum ent objects. M ost events that
a document will receive are events that have been fired at the elements it contains and
have bubbled up to the document. Hence, the Document object suppo rts all of the
Element’s event-handler properties, some of which are listed on page 4 02.
Event bubbling, or event propagation, is a mechanism that fires an event not only at
the e le ment on which it occurs but also on all its ancestors up the document tree.
Event bubbling allows you to register a single event handler on a docume nt or some
other container eleme nt, which is som etimes more convenient than to do that o n each
element separately.
E.8 Element (Client-Side JavaScript)
Each element in an HTML document can be represented by an Eleme nt object in
JavaScript code. The properties and methods of Elem ent allow you to query and
manipulate elements positioned on a web page. Since Element is a subclass of Node,
you can use the properties and methods of Node on Element objects as well. A very
important subject connected with Element are events. Web browsers fire numerous
kinds of events on HTML elemen ts, and Element has defined different event-handler
properties to handle those events.
Object Instance Creation
The Element object defines no constructor of its own, but you can create elements
using the createElement() method of Document. After creating a new elem ent,
you must insert it into the document using method s like appendChild() of the Nod e
object.
You can also obtain the elements that already exist on a web page. Listed under the
Document obje ct, you will find properties like head and body, which return spe-
cific elements, and also many methods that allow you to obtain eleme nts accord-
ing to different criter ia like getElementById( ), getElementsByT agName(), or
getElementsByClassName(), for example . Note that some of th ose methods are
defined not only on Document but also on Element.
Properties
In this section, you will find listed some of the properties that Element defines. Apart
from these, you can also use the properties listed under Node (as Element is a subclass
of Node) and properties whic h, for the most part, have the same names as attributes of
correspo nding HTML elements.
396 JavaScript Reference