Java Reference
In-Depth Information
The fi rst of the main objects you looked at was the
window object. This sits at the very top of
the BOM's hierarchy. The window object contains a number of important sub-objects, including
the location object, the navigator object, the history object, the screen object, and the
document object.
The
location object contains information about the current page's location, such as its fi le name,
the server hosting the page, and the protocol used. Each of these is a property of the location
object. Some properties are read-only, but others, such as the href property, not only enable us
to fi nd the location of the page but can be changed so that we can navigate the page to a new
location.
The
history object is a record of all the pages the user has visited since opening his or her
browser. Sometimes pages are not noted (for example, when the location object's replace()
method is used for navigation). You can move the browser forward and backward in the history
stack and discover what pages the user has visited.
The
navigator object represents the browser itself and contains useful details of what type of
browser, version, and operating system the user has. These details enable you to write pages
dealing with various types of browsers, even where they may be incompatible.
The
screen object contains information about the display capabilities of the user's computer.
The
document object is one of the most important objects. It's an object representation of your
page and contains all the elements, also represented by objects, within that page. The differences
between the various browsers are particularly prominent here, but there are similarities between
the browsers that enable you to write cross-browser code.
The
document object contains three properties that are actually collections. These are the links,
images, and forms collections. Each contains all the objects created by the <a/>, <img/>, and
<form/> elements on the page, and it's a way of accessing those elements.
The
images collection contains an img object for each <img/> element on the page. You found
that even after the page has loaded, you can change the properties of images. For example, you
can make the image change when clicked. The same principles for using the images collection
apply to the links collection.
You next saw that BOM objects have events as well as methods and properties. You handle
these events in JavaScript by using event handlers, which you connect to code that you want to
have executed when the event occurs. The events available for use depend on the object you are
dealing with.
Connecting a function that you have written to an event handler is simply a matter of adding an
attribute to the element corresponding to the particular object you are interested in. The attri-
bute has the name of the event handler you want to capture and the value of the function you
want to connect to it.
In some instances, such as for the
document object, a second way of connecting event handlers
to code is necessary. Setting the object's property with the name of the event handler to your
function produces the same effect as if you did it using the event handler as an attribute.
In some instances, returning values from event functions enables you to cancel the action associ-
ated with the event. For example, to stop a clicked link from navigating to a page, you return
false from the event handler's code.
Search WWH ::




Custom Search