Section 25.195. Link: a hyperlink or anchor in an HTML document


25.195. Link: a hyperlink or anchor in an HTML document

DOM Level 0: Node Element HTMLElement Link

25.195.1. Properties

The most important property of a Link is its href, which is the URL to which it links. The Link object also defines a number of other properties that hold portions of the URL. For each of these properties, the example given is a portion of the following (fictitious) URL:

 http://www.oreilly.com:1234/catalog/search.html?q=JavaScript&m=10#results 


String hash

Specifies the anchor portion of the Link's URL, including the leading hash (#) markfor example, "#results". This anchor portion of a URL refers to a named position within the document referenced by the Link. In HTML files, positions are named with the name attribute of the <a> tag. (see Anchor).


String host

Specifies the hostname and port portions of a Link's URLfor example, "www.oreilly.com:1234".


String hostname

Specifies the hostname portion of a Link's URLfor example, "www.oreilly.com".


String href

Specifies the complete text of the Link's URL, unlike other Link URL properties that specify only portions of the URL.


String pathname

Specifies the pathname portion of a Link's URLfor example, "/catalog/search.html".


String port

Specifies the port portion of a Link's URLfor example, "1234".


String protocol

Specifies the protocol portion of a Link's URL, including the trailing colonfor example, "http:".


String search

Specifies the query portion of a Link's URL, including the leading question markfor example, "?q=JavaScript&m=10".

In addition to these URL-related properties, Link objects also define properties that correspond to the attributes for the HTML <a> and <area> tags:

Property

Attribute

Description

String accessKey

accesskey

Keyboard shortcut

String charset

charset

Encoding of the destination document

String coords

coords

For <area> tags

String hreflang

hreflang

Language of the linked document

String name

name

Anchor name; see Anchor

String rel

rel

Link type

String rev

rev

Reverse link type

String shape

shape

For <area> tags

long tabIndex

tabindex

Link's position in tabbing order

String target

target

Name of the frame or window in which the destination document is to be displayed

String type

type

Content type of the destination document


25.195.2. Methods


blur( )

Takes keyboard focus away from the link.


focus( )

Scrolls the document so the link is visible and gives keyboard focus to the link.

25.195.3. Event Handlers

The Link object has special behavior for three event handlers:


onclick

Invoked when the user clicks on the link.


onmouseout

Invoked when the user moves the mouse off the link.


onmouseover

Invoked when the user moves the mouse over the link.

25.195.4. HTML Syntax

A Link object is created with standard <a> and </a> tags. The href attribute is required for all Link objects. If the name attribute is also specified, an Anchor object is also created:

 <a href="url"                  // The destination of the link     [ name="anchor_tag" ]      // Creates an Anchor object     [ target="window_name" ]   // Where the new document should be displayed     [ onclick="handler" ]      // Invoked when link is clicked     [ onmouseover="handler" ]  // Invoked when mouse is over link     [ onmouseout="handler" ]   // Invoked when mouse leaves link >link text or image  // The visible part of the link </a> 

25.195.5. Description

A Link object represents a hyperlink in a document. Links are usually created with <a> tags that have an href attribute defined, but they may also be created with <area> tags inside a client-side image map. When an <a> tag has a name attribute instead of an href attribute, it defines a named position in a document and is represented by an Anchor object instead of a Link object. See Anchor for details.

All links in a document (whether created with <a> or <area> tags) are represented by Link objects in the links[] array of the Document object.

The destination of a hypertext link is a URL, of course, and many of the properties of the Link object specify the contents of that URL. In this way, the Link object is similar to the Location object, which also has a full set of URL properties.

25.195.6. Example

 // Get the URL of the first hyperlink in the document var url = document.links[0].href; 

25.195.7. See Also

Anchor, Location




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net