Name

Anchor: the target of a hypertext link — DOM Level 0: Node → Element → HTMLElement → Anchor

Properties

Stringname

Contains the name of an Anchor object. The value of this property is set by the name attribute of the <a> tag.

Methods

focus( )

Scrolls the document so the location of the anchor is visible.

HTML Syntax

An Anchor object is created by any standard HTML <a> tag that contains a name attribute:

<a name="name">  // Links may refer to this anchor by this name
    ...
</a>

Description

An anchor is a named location within an HTML document. Anchors are created with an <a> tag that has a name attribute specified. The Document object has an anchors[] array property that contains Anchor objects that represent each of the anchors in the document. Anchor objects can be referenced by index or by name within this array.

You can make a browser display the location of an anchor by setting the hash property of the Location object to a # character followed by the name of the anchor or by simply calling the focus( ) method of the Anchor object itself.

Note that the <a> tag used to create anchors is also used to create hypertext links. Although hypertext links are often called anchors in HTML parlance, they are represented in JavaScript with the Link object, not with the Anchor object.

Example

// Scroll the document so the anchor named "_bottom_" is visible
document.anchors['_bottom_'].focus( );

See Also

Document, Link, Location

Get JavaScript: The Definitive Guide, 5th 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.