DHTML Collections
DOM Level 1 Specification.
Retrieves a collection of all the anchor ( A ) elements in a document with a value for the name attribute.
[ collAnchors = ] document.anchors |
[ oObject = ] document.anchors ( vIndex [ , iSubIndex ] ) |
collAnchors |
Reference to an array of elements contained by the object. |
oObject |
Reference to an individual item in the array of elements contained by the object. |
vIndex |
Required. Number or string specifying the element or collection to retrieve. If this parameter is a number, the method returns the element in the collection at the given position, where the first element has value 0, the second has 1, and so on. If this parameter is a string and there is more than one element with the name or id property equal to the string, the method returns a collection of matching elements. |
iSubIndex |
Optional. Position of an element to retrieve. This parameter is used when vIndex is a string. The method uses the string to construct a collection of all elements that have a name or id equal to the string and then retrieves from this collection the element at the position specified by iSubIndex. |
For reasons of backwards compatibility, the DOM specifies that the returned set of anchors only contain those anchors created with the name attribute, not those created with the id attribute.
Internet Explorer®, though, supports both the name and id attributes in anchors and thus returns a collection that contains anchors created with both attributes. Netscape Navigator® returns a set of anchors with a value only for the name attribute.
As with any collection, objects in the collection are listed in HTML source order.
This collection is indexed first by name, then by identifier. If duplicate names are found, a collection of those named items is returned. Collections of duplicate names must subsequently be referenced by ordinal position.
The following example shows how the anchors collection is used to return the name values of all the A elements used as bookmarks in this document.
<script language="JavaScript">
<!--
function getAnchors ( ) {
var url = "This page contains the following anchors:\n";
for ( i = 0; i < document.anchors.length; i ++ )
url += "\n" + document.anchors [ i ].name;
alert ( url );
}
//-->
</script>
Show me
document
Authoring Your Web Pages: Anchors and Links