<HTML> <HEAD> <TITLE> </TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> <FONT SIZE=-1><A HREF="contents.htm">Table of Contents</A> | <A HREF="objintro.htm">Previous</A> | <A HREF="applet.htm">Next</A> | <A HREF="bklast.htm">Index</A> </FONT><BR><BR> <HR> <H1><A NAME="Anchor"></A> <A NAME="1193137"> Anchor </A></H1> <A NAME="1193147"> A place in a document that is the target of a hypertext link.<TABLE BORDER="0"> <TR><TD VALIGN=baseline ALIGN=left><P><A NAME="1193140"> <I>Client-side object</I></A></P><TD VALIGN=baseline ALIGN=left><P><A NAME="1193142"> </A></P> <TR><TD VALIGN=baseline ALIGN=left><P><A NAME="1193144"> <I>Implemented in</I></A></P><TD VALIGN=baseline ALIGN=left><P><A NAME="1195906"> JavaScript 1.0</A></P><P><A NAME="1195913"> </A></P><P><A NAME="1195928"> JavaScript 1.2: added <A HREF="anchor.htm#1194892"><CODE>name</CODE></A>, <A HREF="anchor.htm#1195585"><CODE>text</CODE></A>, <A HREF="anchor.htm#1195010"><CODE>x</CODE></A>, and <A HREF="anchor.htm#1195074"><CODE>y</CODE></A> properties</A></P> </TABLE> </A></P> <H4><A NAME="Head3;"></A> <A NAME="1194283"> Created by </A></H4> <A NAME="1194289"> Using the HTML <CODE><A HREF="http://developer.netscape.com/docs/manuals/htmlguid/index.htm?content=tags7.htm#tags:A" TARGET="_top">A</A></CODE> tag or calling the <A HREF="string.htm#1196382"><CODE>String.anchor</CODE></A> method. The JavaScript runtime engine creates an <CODE>Anchor</CODE> object corresponding to each <CODE>A</CODE> tag in your document that supplies the <CODE>NAME</CODE> attribute. It puts these objects in an array in the <A HREF="document.htm#1193482"><CODE>document.anchors</CODE></A> property. You access an <CODE>Anchor</CODE> object by indexing this array.</A></P> <A NAME="1193161"> To define an anchor with the <A HREF="string.htm#1196382"><CODE>String.anchor</CODE></A> method:</A></P> <PRE><A NAME="1193162"><I>theString</I>.anchor(<I>nameAttribute</I>)</A></PRE><A NAME="1193175"> <VAR>where:</VAR><TABLE BORDER="0"> <TR><TD VALIGN=baseline ALIGN=left><PRE><A NAME="1193446">theString</A></PRE><TD VALIGN=baseline ALIGN=left><P><A NAME="1193451"> A <A HREF="string.htm#1193137"><CODE>String</CODE></A> object.</A></P> <TR><TD VALIGN=baseline ALIGN=left><PRE><A NAME="1193453">nameAttribute</A></PRE><TD VALIGN=baseline ALIGN=left><P><A NAME="1193455"> A string.</A></P> </TABLE> </A></P> <A NAME="1193176"> To define an anchor with the <CODE>A</CODE> tag, use standard HTML syntax. If you specify the <CODE>NAME</CODE> attribute, you can use the value of that attribute to index into the <CODE>anchors</CODE> array.</A></P> <H4><A NAME="Head3;"></A> <A NAME="1193177"> Description </A></H4> <A NAME="1193178"> If an <CODE>Anchor</CODE> object is also a <CODE>Link</CODE> object, the object has entries in both the <CODE>anchors</CODE> and <CODE>links</CODE> arrays.</A></P> <H4><A NAME="Head3;"></A> <A NAME="1194025"> Property Summary </A></H4> <A NAME="1194863"> <P><B></B> <TABLE BORDER="2" CELLPADDING=5> <TR><TH VALIGN=baseline ALIGN=left><B><A NAME="1194676"> <B>Property </B></A><B><TH VALIGN=baseline ALIGN=left><B><A NAME="1194678"> <B>Description </B></A><B> <TR><TD VALIGN=baseline ALIGN=left><PRE><A NAME="1194683"><A HREF="anchor.htm#1194892"><CODE>name</CODE></A></A></PRE><TD VALIGN=baseline ALIGN=left><P><A NAME="1194685"> A string specifying the anchor's name.</A></P> <TR><TD VALIGN=baseline ALIGN=left><PRE><A NAME="1195186"><A HREF="anchor.htm#1195585"><CODE>text</CODE></A></A></PRE><TD VALIGN=baseline ALIGN=left><P><A NAME="1195188"> A string specifying the text of an anchor.</A></P> <TR><TD VALIGN=baseline ALIGN=left><PRE><A NAME="1195193"><A HREF="anchor.htm#1195010"><CODE>x</CODE></A></A></PRE><TD VALIGN=baseline ALIGN=left><P><A NAME="1195195"> The horizontal position of the anchor's left edge, in pixels, relative to the left edge of the document.</A></P> <TR><TD VALIGN=baseline ALIGN=left><PRE><A NAME="1195200"><A HREF="anchor.htm#1195074"><CODE>y</CODE></A></A></PRE><TD VALIGN=baseline ALIGN=left><P><A NAME="1195202"> The vertical position of the anchor's top edge, in pixels, relative to the top edge of the document.</A></P> </TABLE> <TABLE> <TR><TD> </TABLE> </A></P> <H4><A NAME="Head3;"></A> <A NAME="1193181"> Method Summary </A></H4> <A NAME="1193849"> This object inherits the <A HREF="object.htm#1193628"><CODE>watch</CODE></A> and <A HREF="object.htm#1193499"><CODE>unwatch</CODE></A> methods from <A HREF="object.htm#1193136"><CODE>Object</CODE></A>.</A></P> <H4><A NAME="Head3;"></A> <A NAME="1193183"> Examples </A></H4> <A NAME="1193184"> <B>Example 1: An anchor.</B> The following example defines an anchor for the text "Welcome to JavaScript":</A></P> <PRE><A NAME="1193185"><A NAME="javascript_intro"><H2>Welcome to JavaScript</H2></A></A></PRE><A NAME="1193186"> If the preceding anchor is in a file called <CODE>intro.html</CODE>, a link in another file could define a jump to the anchor as follows:</A></P> <PRE><A NAME="1193187"><A HREF="intro.html#javascript_intro">Introduction</A></A></PRE><A NAME="1193188"> <B>Example 2: anchors array.</B> The following example opens two windows. The first window contains a series of buttons that set <CODE>location.hash</CODE> in the second window to a specific anchor. The second window defines four anchors named "0," "1," "2," and "3." (The anchor names in the document are therefore 0, 1, 2, ... (document.anchors.length-1).) When a button is pressed in the first window, the <CODE>onClick</CODE> event handler verifies that the anchor exists before setting <CODE>window2.location.hash</CODE> to the specified anchor name.</A></P> <A NAME="1193189"> <CODE>link1.html</CODE>, which defines the first window and its buttons, contains the following code:</A></P> <PRE><A NAME="1193190"><HTML><br><HEAD><br><TITLE>Links and Anchors: Window 1</TITLE><br></HEAD><br><BODY><br><SCRIPT><br>window2=open("link2.html","secondLinkWindow",<br> "scrollbars=yes,width=250, height=400")<br>function linkToWindow(num) {<br> if (window2.document.anchors.length > num)<br> window2.location.hash=num<br> else<br> alert("Anchor does not exist!")<br>}<br></SCRIPT></A></PRE><PRE><A NAME="1194342"><B>Links and Anchors</B><br><FORM><br><P>Click a button to display that anchor in window #2<br><P><INPUT TYPE="button" VALUE="0" NAME="link0_button"<br> onClick="linkToWindow(this.value)"><br><INPUT TYPE="button" VALUE="1" NAME="link0_button"<br> onClick="linkToWindow(this.value)"><br><INPUT TYPE="button" VALUE="2" NAME="link0_button"<br> onClick="linkToWindow(this.value)"><br><INPUT TYPE="button" VALUE="3" NAME="link0_button"<br> onClick="linkToWindow(this.value)"><br><INPUT TYPE="button" VALUE="4" NAME="link0_button"<br> onClick="linkToWindow(this.value)"><br></FORM><br></BODY><br></HTML></A></PRE><A NAME="1193191"> <CODE>link2.html</CODE>, which contains the anchors, contains the following code:</A></P> <PRE><A NAME="1193192"><HTML><br><HEAD><br><TITLE>Links and Anchors: Window 2</TITLE><br></HEAD><br><BODY><br><A NAME="0"><B>Some numbers</B> (Anchor 0)</A><br><UL><LI>one<br><LI>two<br><LI>three<br><LI>four</UL><br><P><A NAME="1"><B>Some colors</B> (Anchor 1)</A><br><UL><LI>red<br><LI>orange<br><LI>yellow<br><LI>green</UL><br><P><A NAME="2"><B>Some music types</B> (Anchor 2)</A><br><UL><LI>R&B<br><LI>Jazz<br><LI>Soul<br><LI>Reggae<br><LI>Rock</UL><br><P><A NAME="3"><B>Some countries</B> (Anchor 3)</A><br><UL><LI>Afghanistan<br><LI>Brazil<br><LI>Canada<br><LI>Finland<br><LI>India</UL><br></BODY><br></HTML></A></PRE> <H4><A NAME="Head3;"></A> <A NAME="1193196"> See also </A></H4> <A NAME="1193200"> <A HREF="link.htm#1193137"><CODE>Link</CODE></A></A></P> <HR><H2><A NAME="Head2Ref;"></A> <A NAME="1194892"> name </A></H2> <A NAME="1194893"> A string specifying the anchor's name.<TABLE BORDER="0"> <TR><TD VALIGN=baseline ALIGN=left><P><A NAME="1194896"> <I>Property of</I></A></P><TD VALIGN=baseline ALIGN=left><P><A NAME="1194901"> <A HREF="anchor.htm#1193137"><CODE>Anchor</CODE></A></A></P> <TR><TD VALIGN=baseline ALIGN=left><P><A NAME="1195338"> <I>Read-only</I></A></P><TD VALIGN=baseline ALIGN=left><P><A NAME="1195340"> </A></P> <TR><TD VALIGN=baseline ALIGN=left><P><A NAME="1194903"> <I>Implemented in</I></A></P><TD VALIGN=baseline ALIGN=left><P><A NAME="1194905"> JavaScript 1.2</A></P> </TABLE> </A></P> <H4><A NAME="Head3;"></A> <A NAME="1194906"> Description </A></H4> <A NAME="1194907"> The <CODE>name</CODE> property reflects the value of the <CODE>NAME</CODE> attribute.</A></P> <H4><A NAME="Head3;"></A> <A NAME="1195580"> Examples </A></H4> <A NAME="1195581"> The following example displays the name of the first anchor in a document:</A></P> <PRE><A NAME="1195582">alert("The first anchor is " + document.anchors[0].name)</A></PRE> <HR><H2><A NAME="Head2Ref;"></A> <A NAME="1195585"> text </A></H2> <A NAME="1194968"> A string specifying the text of an anchor.<TABLE BORDER="0"> <TR><TD VALIGN=baseline ALIGN=left><P><A NAME="1194958"> <I>Property of</I></A></P><TD VALIGN=baseline ALIGN=left><P><A NAME="1194963"> <A HREF="anchor.htm#1193137"><CODE>Anchor</CODE></A></A></P> <TR><TD VALIGN=baseline ALIGN=left><P><A NAME="1195346"> <I>Read-only</I></A></P><TD VALIGN=baseline ALIGN=left><P><A NAME="1195348"> </A></P> <TR><TD VALIGN=baseline ALIGN=left><P><A NAME="1194965"> <I>Implemented in</I></A></P><TD VALIGN=baseline ALIGN=left><P><A NAME="1194967"> JavaScript 1.2</A></P> </TABLE> </A></P> <H4><A NAME="Head3;"></A> <A NAME="1194969"> Description </A></H4> <A NAME="1194970"> The <CODE>text</CODE> property specifies the string that appears within the <CODE>A</CODE> tag.</A></P> <H4><A NAME="Head3;"></A> <A NAME="1194977"> Examples </A></H4> <A NAME="1195593"> The following example displays the text of the first anchor in a document:</A></P> <PRE><A NAME="1195594">alert("The text of the first anchor is " + document.anchors[0].text)</A></PRE> <HR><H2><A NAME="Head2Ref;"></A> <A NAME="1195010"> x </A></H2> <A NAME="1195023"> The horizontal position of the anchor's left edge, in pixels, relative to the left edge of the document.<TABLE BORDER="0"> <TR><TD VALIGN=baseline ALIGN=left><P><A NAME="1195013"> <I>Property of</I></A></P><TD VALIGN=baseline ALIGN=left><P><A NAME="1195018"> <A HREF="anchor.htm#1193137"><CODE>Anchor</CODE></A></A></P> <TR><TD VALIGN=baseline ALIGN=left><P><A NAME="1195354"> <I>Read-only</I></A></P><TD VALIGN=baseline ALIGN=left><P><A NAME="1195356"> </A></P> <TR><TD VALIGN=baseline ALIGN=left><P><A NAME="1195020"> <I>Implemented in</I></A></P><TD VALIGN=baseline ALIGN=left><P><A NAME="1195022"> JavaScript 1.2</A></P> </TABLE> </A></P> <H4><A NAME="Head3;"></A> <A NAME="1195037"> See also </A></H4> <A NAME="1195047"> <A HREF="anchor.htm#1195074"><CODE>Anchor.y</CODE></A></A></P> <HR><H2><A NAME="Head2Ref;"></A> <A NAME="1195074"> y </A></H2> <A NAME="1195087"> The vertical position of the anchor's top edge, in pixels, relative to the top edge of the document.<TABLE BORDER="0"> <TR><TD VALIGN=baseline ALIGN=left><P><A NAME="1195077"> <I>Property of</I></A></P><TD VALIGN=baseline ALIGN=left><P><A NAME="1195082"> <A HREF="anchor.htm#1193137"><CODE>Anchor</CODE></A></A></P> <TR><TD VALIGN=baseline ALIGN=left><P><A NAME="1195362"> <I>Read-only</I></A></P><TD VALIGN=baseline ALIGN=left><P><A NAME="1195364"> </A></P> <TR><TD VALIGN=baseline ALIGN=left><P><A NAME="1195084"> <I>Implemented in</I></A></P><TD VALIGN=baseline ALIGN=left><P><A NAME="1195086"> JavaScript 1.2</A></P> </TABLE> </A></P> <H4><A NAME="Head3;"></A> <A NAME="1195137"> See also </A></H4> <A NAME="1195141"> <A HREF="anchor.htm#1195010"><CODE>Anchor.x</CODE></A></A></P> <HR> <FONT SIZE=-1><A HREF="contents.htm">Table of Contents</A> | <A HREF="objintro.htm">Previous</A> | <A HREF="applet.htm">Next</A> | <A HREF="bklast.htm">Index</A> </FONT> <P ALIGN=right> <FONT SIZE=-2><I>Last Updated: 05/28/99 11:58:58</I></FONT> <P> <CENTER>Copyright (c) 1999 <A HREF="http://home.netscape.com/misc/contact_info.html" TARGET=_top>Netscape Communications Corporation</A></FONT> </CENTER> <P> </BODY> </HTML>