Anchor object (Object/HTML)

An object representing an HTML <A> tag.

Availability:

DOM level - 1
JavaScript - 1.2
JScript - 3.0
Internet Explorer - 4.0
Netscape - 4.0
Inherits from:Element object
JavaScript syntax:-myAnchor = myAnchorArray[aName]
-myAnchor = myAnchorArray[anIndex]
-myAnchor = myDocument.anchors[aName]
-myAnchor = myDocument.anchors[anIndex]
-myAnchor = myDocument.getElementById(anElementID)
-myAnchor = myDocument.getElementsByName(aName)[anIndex]
-myAnchor = myDocument.getElementsByTagName("A")[anIndex]
-myAnchor = myDocument.links[aName]
-myAnchor = myDocument.links[anIndex]
IEmyAnchor = myDocument.all.anElementID
IEmyAnchor = myDocument.all.tags("A")[anIndex]
IEmyAnchor = myDocument.all[aName]
IEmyAnchor = myDocument.anchors.item(aName)[anIndex]
IEmyAnchor = myDocument.links.item(aName)[anIndex]
HTML syntax:<A> ... </A>
Argument list:aNameAn associative array reference to the anchor object.
aNameThe name property of the anchor object
anIndexAn index into the anchors collection
someTextThe text (or innerText) property of the anchor
anElementIDThe ID value of an Element object
Object properties:accessKey, charset, coords, dataFld, dataSrc, hash, host, hostname, href, hreflang, Methods, mimeType, name, nameProp, pathname, port, protocol, protocolLong, recordNumber, rel, rev, search, shape, tabIndex, target, text, type, urn, x, y
Object methods:blur(), focus()
Event handlers:onClick, onMouseDown, onMouseOut, onMouseOver, onMouseUp

This object represents a named location in the HTML document. Only those <A> HTML tags that have a NAME attribute will have anchor objects created for them. All the anchors are listed in the anchors[] array object that belongs to the document object that represents the HTML.

Although the <A> tag is also used to create links using the HREF attribute, they are not anchors unless they are named. Any <A> tags that have HREF attributes (whether or not they have NAME attributes) will be listed in the links[] array.

In Netscape, you can construct new instances of the Anchor object, but there is no constructor property in MSIE to support this.

<A> tags and the objects that represent them are inline elements. Placing them into a document does not create a line break.

Warnings:

Example code:

   <!-- Example showing how to dynamically replace -->
   <!-- the anchor text -->
   <HTML>
   <HEAD>
   </HEAD>
   <BODY>
   <A NAME="A1" HREF="www.apple.com">Click here</A><BR>
   <A NAME="A2" HREF="www.wrox.com">Click here</A><BR>
   <A NAME="A3" HREF="www.msdn.com">Click here</A><BR>
   <BR>
   <HR>
   <SCRIPT>
   myLength = document.anchors.length;
   for (myEnumerator=0; myEnumerator<myLength; myEnumerator++ )
   {
      document.anchors[myEnumerator].innerText = document.anchors[myEnumerator].name;
   }
   </SCRIPT>
   </BODY>
   </HTML>

See also:Document.anchors[], Document.links[], Element object, Element.all[], Input.accessKey, LINK object, Location object, String.anchor(), Subclasses, Superclasses, URL, Url object, Window.scrollTo()

PropertyJavaScriptJScriptNIEOperaNESECMADOMCSSHTMLNotes
accessKey1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a1 1 n/a n/aWarning
charset1.5 1.55.0 5.06.0 6.05.0 5.0 n/a n/a n/a1 1 n/a n/a-
coords1.5 1.55.0 5.06.0 6.05.0 5.0 n/a n/a n/a1 1 n/a n/a-
dataFld n/a3.0 3.0 n/a4.0 4.0 n/a n/a n/a n/a n/a n/a-
dataSrc n/a3.0 3.0 n/a4.0 4.0 n/a n/a n/a n/a n/a n/a-
hash1.2 1.23.0 3.04.0 4.04.0 4.0 n/a n/a n/a n/a n/a n/aWarning
host1.2 1.23.0 3.04.0 4.04.0 4.0 n/a n/a n/a n/a n/a n/aWarning
hostname1.2 1.23.0 3.04.0 4.04.0 4.0 n/a n/a n/a n/a n/a n/aWarning
href1.2 1.23.0 3.04.0 4.04.0 4.0 n/a n/a n/a1 1 n/a n/aWarning
hreflang1.5 1.55.0 5.06.0 6.05.0 5.0 n/a n/a n/a1 1 n/a n/a-
Methods n/a3.0 3.0 n/a4.0 4.0 n/a n/a n/a n/a n/a n/a-
mimeType n/a3.0 3.0 n/a4.0 4.0 n/a n/a n/a n/a n/a n/aWarning , ReadOnly
name1.2 1.23.0 3.04.0 4.04.0 4.0 n/a n/a n/a1 1 n/a n/aWarning
nameProp n/a3.0 3.0 n/a4.0 4.0 n/a n/a n/a n/a n/a n/aReadOnly
pathname1.2 1.23.0 3.04.0 4.04.0 4.0 n/a n/a n/a n/a n/a n/aWarning
port1.2 1.23.0 3.04.0 4.04.0 4.0 n/a n/a n/a n/a n/a n/aWarning
protocol1.2 1.23.0 3.04.0 4.04.0 4.0 n/a n/a n/a n/a n/a n/aWarning
protocolLong n/a3.0 3.0 n/a4.0 4.0 n/a n/a n/a n/a n/a n/aReadOnly
recordNumber n/a3.0 3.0 n/a4.0 4.0 n/a n/a n/a n/a n/a n/aReadOnly
rel1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a1 1 n/a n/a-
rev1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a1 1 n/a n/a-
search1.2 1.23.0 3.04.0 4.04.0 4.0 n/a n/a n/a n/a n/a n/aWarning
shape1.5 1.55.0 5.06.0 6.05.0 5.0 n/a n/a n/a1 1 n/a n/a-
tabIndex1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a1 1 n/a n/a-
target1.2 1.23.0 3.04.0 4.04.0 4.0 n/a n/a n/a1 1 n/a n/aWarning
text1.2 1.2 n/a4.0 4.0 n/a n/a n/a n/a n/a n/a n/aWarning , ReadOnly
type1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a1 1 n/a n/a-
urn n/a3.0 3.0 n/a4.0 4.0 n/a n/a n/a n/a n/a n/a-
x1.2 1.2 n/a4.0 4.0 n/a n/a n/a n/a n/a n/a n/aWarning , ReadOnly
y1.2 1.2 n/a4.0 4.0 n/a n/a n/a n/a n/a n/a n/aWarning , ReadOnly

MethodJavaScriptJScriptNIEOperaNESECMADOMCSSHTMLNotes
blur()1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a1 1 n/a n/a-
focus()1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a1 1 n/a n/a-

Event nameJavaScriptJScriptNIEOperaNESECMADOMCSSHTMLNotes
onClick1.21.23.0 3.04.0 4.04.0 4.03.0 3.0 n/a n/a n/a n/a4.0 4.0Warning
onMouseDown1.2 1.23.0 3.04.0 4.04.0 4.03.0 3.0 n/a n/a n/a n/a4.0 4.0Warning
onMouseOut1.2 1.23.0 3.04.0 4.04.0 4.03.0 3.0 n/a n/a n/a n/a4.0 4.0Warning
onMouseOver1.2 1.23.0 3.04.0 4.04.0 4.03.0 3.0 n/a n/a n/a n/a4.0 4.0Warning
onMouseUp1.2 1.23.0 3.04.0 4.04.0 4.03.0 3.0 n/a n/a n/a n/a4.0 4.0Warning

Inheritance chain:

Element object, Node object