JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

Link Object

Name

Link Object---a hypertext link

Availability

Navigator 2.0, Internet Explorer 3.0; enhanced in Navigator 3.0

Synopsis

document.links[]
document.links.length

Properties

hash

A read/write string that specifies the hash portion of the HREF URL, including the leading hash (#) mark. This portion specifies the name of an anchor within the object referred to by the URL.

host

A read/write string that specifies the combination of the hostname and port portions of the HREF URL.

hostname

A read/write string that specifies the hostname portion of the HREF URL.

href

A read/write string that specifies the complete URL specified by the HREF property.

pathname

A read/write string that specifies the path portion of the HREF URL.

port

A read/write string that specifies the port portion of the HREF URL.

protocol

A read/write string that specifies the protocol portion of the HREF URL, including the trailing colon.

search

A read/write string that specifies the search or query portion of the HREF URL, including the leading question mark.

target

A read/write string property that specifies the name of a Window object (i.e., a frame or a top-level browser window) in which the HREF URL should be displayed.

Event Handlers

onclick

Statements invoked when the user clicks on the link. In Navigator 3.0, this event handler may prevent the link from being followed (e.g., after asking the user to confirm) by returning false. On Windows platforms in Navigator 3.0, this event handler does not work for links created with the <AREA> tag.

onmouseout

Statements invoked when the user moves the mouse off of the link. Available in Navigator 3.0 and later.

onmouseover

Statements invoked when the user moves the mouse over the link. The status property of the current window may be set here.

HTML Syntax

A Link object is created with standard <A> and </A> tags, with the addition of the onClick, onMouseOver and onMouseOut event-handler attributes. The HREF attribute is required for all Link objects. If the NAME attribute is also specified, then 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>

In Navigator 3.0 and later, a Link object is also created by each <AREA> tag within a client-side image map. This is also standard HTML with the addition of event-handler tags:

<MAP NAME="map_name">
    <AREA SHAPE="area_shape"
        COORDS=coordinates
        HREF="url"    the destination of the link
        [ TARGET="window_name" ]  where the new document should be displayed
        [ onClick="handler" ] invoked when area is clicked
        [ onMouseOver="handler" ] invoked when mouse is over area
        [ onMouseOut="handler" ]  invoked when mouse leaves area
>
    ... 
</MAP>

Description

The Link object represents a hypertext link or a clickable area of a client-side image map in an HTML document. All links created with the <A> (and in Navigator 3.0 the <AREA>) tag are represented by Link objects and stored in the links[] array of the Document object. Note that links created by both the <A> and <AREA> tags are stored in the same array--there is no real distinction between them, and there is no special Area object that represents hypertext links in an image map.

The Link object can be thought of as a specialized form of the URL object. The destination of a hypertext link is a URL, of course, and the Link object has all of the properties of the URL object which describe that destination. These protocol, hostname, pathname and other URL properties are all documented under the URL object. The Link object is also related to the Location object, which also has a full set of URL properties that describe the URL of the currently displayed document.

The Link object has an additional target property and three event handlers that are not shared by the URL object or Location objects, however. The target property specifies a window name into which the document referred to by the hypertext link should be loaded. And the onmouseover(), onclick(), and onmouseout() event handlers specify code to be executed when the mouse passes over the hypertext link, clicks on it, and moves off or out of the link's region of the screen.

Usage

In HTML parlance, hypertext links are sometimes called "anchors" because they are created with the <A> tag, which is used both for links to other URLs and for creating named link destinations within a document. This is a confusing usage that should be avoided; in JavaScript, a hypertext link is a Link object, and a named destination for a link is an Anchor object.

See Also

"Anchor", "Area", "Location", "URL"


Previous Home Next
JSObject.toString() Book Index Link.hash

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell
Hosted by uCoz