The <A> Element: Hyperlinks and Anchors You create hyperlinks with the <A> element, of course, as well as HTML anchors that can serve as hyperlink targets. Like the location object (see Chapter 10, "Using the navigator , location , and history Objects"), this element not only holds the target of the hyperlink in the href property, but also enables you to dissect that URL in a handy way, using several properties such as hostname (the www. name .com part), host (the same as the hostname property with a port number appended if there is onefor example, in the URL http://www. name .com:80/index.html , the hostname is www. name .com:80 ), href (the current URL in full), hash (the part of the URL following the hash mark (#) in the URL if there is one), and pathname (the filename or path specified in the URL). Here's the URL we dissected when looking at the location object in Chapter 10 (it discusses the changes in the HTML 4.01 W3C specification): http://www.w3.org/TR/html4/appendix/changes.html#19991224 Here are the values of various location properties for this URL: For more on these properties, take a look at Table 10.8. Let's turn to the <A> element in detail now. You can find its properties in Table 14.1, and its events in Table 14.2. As usual, note also that JavaScript's core HTML properties, methods , and events that we covered in Chapters 5, "Using Core HTML Properties," and 6, "Using Core HTML Methods and Events," apply to this element as well. Table 14.1. The Properties of the <A> Element (See Chapters 5 and 6 for the JavaScript core HTML properties, methods, and events that also apply to this element.) Property | NS2 | NS3 | NS4 | NS6 | IE3a | IE3b | IE4 | IE5 | IE5.5 | IE6 | charset | | | | x | | | | | | x | | Read/write | | Holds the name of the character set used by the document to which the hyperlink points. In the HTML 4.01 specification, the W3C says that you can get a list of the registered character sets at ftp://ftp.isi.edu/in-notes/iana/assignments/character-sets. Not in wide use yet. | coords | | | | x | | | | | | x | | Read/write | | This property is used in creating image maps with given coordinates in an image. Although listed as supported, this property does not actually seem to work in Netscape Navigator 6.0. In the Internet Explorer, set to a string with the coordinates for the image map. | hash | x | x | x | x | x | x | x | x | x | x | | Read/write | | See Table 10.8. | host | x | x | x | x | x | x | x | x | x | x | | Read/write | | See Table 10.8. | hostname | x | x | x | x | x | x | x | x | x | x | | Read/write | | See Table 10.8. | href | x | x | x | x | x | x | x | x | x | x | | Read/write | | This property holds the target of the hyperlink; Set it to a URL string. You can change this property on-the-fly , which is cool, enabling you to configure the hyperlinks in your page in response to user action (such as selecting between web pages written in different languages). See "Using Hyperlinks" in this chapter for an example, including using a JavaScript URL. | hreflang | | | | x | | | | | | x | | Read/write | | This property holds the language of the target document pointed to by this element. Set to a string; see "Using the browserLanguage , systemLanguage , and userLanguage Properties" in Chapter 10 for possible values. | Methods | | | | | | | x | x | x | x | | Read/write | | Sets or retrieves the list of HTTP methods supported by the object. The uppercase M is not an error here. This is one of the few properties that start with an uppercase letter, and it's also a rare HTML 4.01 attribute that is not part of the W3C DOM. This property corresponds to the METHODS attribute of the <A> element. Set to a string containing an HTTP method that the browser should use to access the target document. Note that the Internet Explorer doesn't actually use this information yet. | name | | | x | x | | | x | x | x | x | | Read/write | | This property corresponds to the NAME attribute of an <A> element. You need to set the NAME attribute to make an <A> element function as an anchor. Set to a name string. | nameProp | | | | | | | x | x | x | x | | Read-only | | This property holds the name of the HTML document, as extracted from the URL to which the hyperlink (taken from the href property) points. (Note that if there is a port number, however, that number is returned as part of the name!) This property is a string. | pathname | x | x | x | x | x | x | x | x | x | x | | Read/write | | See Table 10.8. | port | x | x | x | x | x | x | x | x | x | x | | Read/write | | See Table 10.8. | protocol | x | x | x | x | x | x | x | x | x | x | | Read/write | | See Table 10.8. | rel | | | | x | | | x | x | x | x | | Read/write | | This property sets or gets the relationship between the link and its destination, corresponding to the REL attribute. Set to a string containing "Alternate" , "Appendix" , "Bookmark" , "Chapter" , "Contents" , "Copyright" , "Glossary" , "Help" , "Index" , "Next" , "Offline" , "Prev" , "Section" , "Shortcut Icon" , "Start" , "Stylesheet" , or "Subsection" . The rev and rel properties are similar, but they specify relationships in opposite directionsa relationship given by rev from A to B is the same as a relationship given by rel from B to A. | rev | | | | x | | | x | x | x | x | | Read/write | | This property sets or gets the relationship between the link and its destination, corresponding to the REV attribute. Set to a string containing "Alternate" , "Appendix" , "Bookmark" , "Chapter" , "Contents" , "Copyright" , "Glossary" , "Help" , "Index" , "Next" , "Offline" , "Prev" , "Section" , "Shortcut Icon" , "Start" , "Stylesheet" , or "Subsection" . The rev and rel properties are similar, but they specify relationships in opposite directionsa relationship given by rev from A to B is the same as a relationship given by rel from B to A. | search | x | x | x | x | x | x | x | x | x | x | | Read/write | | See Table 10.8. | shape | | | | x | | | | | | x | | Read/write | | This property is used in creating image maps with different shapes , such as polygons, circles, and rectangles. Although listed as supported, this property does not actually seem to work in Netscape Navigator 6.0. | target | x | x | x | x | x | x | x | x | x | x | | Read/write | | Holds the window or frame name that represents the target of this linkthat is, where the document pointed to by this link should be loaded. This property holds a string and corresponds to the TARGET attribute of the <A> element. | text | | | x | | | | | | | | | Read/write | | Navigator 4.0 enables you to read the text for the hyperlink in a web page using this property (that is, the text between the <A> and </A> tags that the user clicks to navigate to the hyperlink's target). Set to a string. I recommend you use the innerHTML property in recent browsers instead. | type | | | | x | | | | | | x | | Read/write | | Holds the Multipurpose Internet Mail Extension (MIME) type of the target document pointed to by the hyperlink. This property corresponds to the <A> element's TYPE attribute, which is not widely usedalthough your scripts can access this property in case you need to handle the target document in some special way. Set to a string containing a MIME type (such as "text/plain" ). | urn | | | | | | | x | x | x | x | | Read/write | | Sets or gets the Uniform Resource Name (URN) for a target document; URNs indicate the identity of a document (rather than URLs, which indicate a document's location). Set to a string. URNs aren't in general use at this time. | x | | | x | | | | | | | | | Read-only | | Holds the X coordinate of the hyperlink in a pagein NS6 and IE4+, you use the offsetLeft property instead. Set to a pixel measurement. See "The offsetHeight , offsetWidth , offsetLeft , offsetRight , and offsetParent Properties" in Chapter 5. | y | | | x | | | | | | | | | Read-only | | Holds the Y coordinate of the hyperlink in a pagein NS6 and IE4+, you use the offsetTop property instead. Set to a pixel measurement. See "The offsetHeight, offsetWidth, offsetLeft, offsetRight, and offsetParent Properties " in Chapter 5. | Table 14.2. The Events of the <A> Element (See Chapters 5 and 6 for the JavaScript core HTML properties, methods, and events that also apply to this element.) Event | NS2 | NS3 | NS4 | NS6 | IE3a | IE3b | IE4 | IE5 | IE5.5 | IE6 | onafterupdate | | | | | | | x | x | x | x | | Occurs in a data-bound object after updating the data in the data source. | onbeforeupdate | | | | | | | x | x | x | x | | Occurs in a data-bound object before updating the data in the data source. | onerrorupdate | | | | | | | x | x | x | x | | Occurs in a data-bound object when an error happened while updating data in the data source. | |