Skip to Content
JavaScript: The Definitive Guide, Fourth Edition
book

JavaScript: The Definitive Guide, Fourth Edition

by David Flanagan
November 2001
Intermediate to advanced
936 pages
68h 43m
English
O'Reilly Media, Inc.
Content preview from JavaScript: The Definitive Guide, Fourth Edition

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

JavaScript: A Beginner's Guide, Fourth Edition, 4th Edition

JavaScript: A Beginner's Guide, Fourth Edition, 4th Edition

John Pollock
JavaScript Cookbook, 3rd Edition

JavaScript Cookbook, 3rd Edition

Adam D. Scott, Matthew MacDonald, Shelley Powers

Publisher Resources

ISBN: 0596000480Supplemental ContentCatalog PageErrata