JavaScript Document Object : anchors property

Description

This property is used to access anchor name. The array stores an entry for each A tag containing a name attribute in a document; these entries are in source order. For example, if a document contains four name anchors whose name attributes are anchor-1, anchor-2, anchor-3, anchor-4 you can refer to the anchors either as:

document.anchors["anchor-1"]
document.anchors["anchor-2"]
document.anchors ["anchor-3"]
document.anchors ["anchor-4"]

or begins as index count started with as:

document.anchors[0]
document.anchors[1]
document.anchors[2]
document.anchors[3].

To get the number of anchors in a document, your can use the length property document.anchors.length.

Version

Implemented in JavaScript 1.0

Syntax

document.anchors["anchor1"]

or

document.anchors[pos]

anchor1 -> Name of anchor in the document.

pos -> Position of the anchor in the document.

Example of Document Object : anchors property

The following web document access and display the anchor names using the anchors array.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript document object - anchor property example</title>
</head>
<body>
<h1 style="color: red">JavaScript document object : anchor property</h1>
<h3> Click on the button to get the Anchor Names.</h3>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
function ShowAnchorName()
{
document.write(document.anchors[0].name+' '+document.anchors[1].name);
document.write(document.anchors[1].name,"<br>");
}
//]]>
</script>
<a name="Anch-1">Anchor-1</a>
<a name="Anch-2">Anchor-2</a>
<br />
<form action="#">
<input name= 'buton' type ='button' value='Button-1'
onclick="ShowAnchorName()"/>
</form>
</body>
</html>

View the example of document object's anchor property in the browser

Practice document object's anchor property with Online Editor

Supported Browser

Internet Explorer 7 Firefox 3.6 Google Chrome 7 Safari 5.0.1 Opera 10
Yes Yes Yes No Yes

See also :

JavaScript Client side objects, methods, properties.

JavaScript Core objects, methods, properties.

Please Google+, Like this tutorial on FaceBook, Tweet, save it as bookmark and subscribe with our Feed. Have suggestions? comment using Disqus down this page. Thanks.


share with delicious share with facebook w3r rss  share with digg share with reddit share with stumbleupon share with twitter