Anchors
The
anchors[]
array of the
Document
object contains Anchor objects representing named locations in the
HTML document that are marked with the <a>
tag and its name
attribute. The anchors[]
array has existed since
JavaScript 1.0, but the Anchor object is new in JavaScript 1.2. In
previous versions, the elements of the anchors[]
array were all undefined, and only the length
property was useful.
The
Anchor object is a simple one. The only standard property it defines
is name
, which is the value of the HTML
name
attribute. As with the Link object, the text
that appears between the <a>
and
</a>
tags of the anchor is specified by
the text
property in
Netscape 4 and by the
innerText
property
in Internet Explorer 4. Neither of these
properties is supported by the W3C DOM standard, but we’ll see
other ways to obtain the text content of an element in Chapter 17.
Example 14-6
shows a function that creates a
navigation window for a specified document. It displays the
text
, innerText
, or
name
of all the anchors in the document. The
anchor text or name is displayed within hypertext
links -- clicking on any anchor causes the original window to
scroll to display that anchor. The code in this example is
particularly useful if you write your HTML documents so that all
section headings are enclosed in anchors. For example:
<a name="sect14.6"><h2>The Anchor Object</h2></a>
Example 14-6. Listing all anchors
/* * FILE: listanchors.js * The function listanchors( ) is passed a document ...
Get JavaScript: The Definitive Guide, Fourth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.