HTML and CSS Reference
In-Depth Information
scripts run, they have access to the contents of the current page, to other pages that are
open, and even to the browser itself. I've mentioned the document object, which provides
access to the contents of the current page.
Now let's look at a specific object. The top-level object in the browser environment is
called window . The window object's children provide information about the various ele-
ments of a web page. Here are some of the most commonly used children of window :
Contains information about the location of the current web
document, including the URL and components of the URL
such as the protocol, domain name, path, and port.
location
Holds a list of all the sites that a web browser has visited
during the current session and also gives you access to
built-in functions that enable you to send the user forward
or back within the history.
history
Contains the complete details of the current web page. All
the tags and content on the page are included in a hierar-
chy under document. Not only can you examine the con-
tents of the page by way of the document object, but you
can also manipulate the page's contents.
document
You can find a complete list of the available objects in the Mozilla JavaScript documen-
tation at http://developer.mozilla.org/en/JavaScript.
Because the entire browser environment is accessible through this hierarchical set of
objects, you can access anything as long as you know where it lives in the hierarchy. For
example, all the links on the current page are stored in the property document.links ,
which contains an array. Each of the elements in the array have their own properties
as well, so to get the location to which the first link in the document points, you use
document.links[0].href .
Events
All the examples you've seen so far are executed as soon as the page loads. JavaScript is
about making your pages more interactive, which means writing scripts that function
based on user input and user activity. To add this interactivity, you need to bind your
JavaScript code to events. The JavaScript environment monitors user activity and pro-
vides the opportunity for you to specify that code will be executed when certain events
occur.
14
There are two ways to bind JavaScript code to an event handler. The first to do it by
way of an attribute on a tag associated with that event handler. The second is to locate
the tag you want to bind the event to in the Document Object Model (DOM) and then
 
 
Search WWH ::




Custom Search