JavaScript » Objects » Link

The Link object is a piece of text, an image or an area of an image that loads a hypertext link reference into the target window when selected.

Area objects are also a type of Link object. A link can be created either by using the HTML 'A' or 'AREA' tags, or by calling the String.link method. Each 'A' or 'AREA' tag that has an HREF attribute is placed by the JavaScript engine in an array in the document.links property. A Link object can then be accessed by indexing this array.

A Link object is a Location object and shares the same properties. When you click a Link object, the destination document's referrer property then contains the URL of the source page. A link can also be used to execute JavaScript code rather than to reference a hyperlink.

Examples

Code:
<script type="text/javascript">
function write_hello()
{
   document.write("Hello World!")
}
</script>
<a href="javascript:write_hello()">GREETINGS</a>
Explanation:

This code simply creates a function to display the message "Hello World!" which is then called if the user clicks on the 'GREETINGS' link.

Code:
document.write("AUTHORS".link("Authors.htm#author"))
Explanation:

This example demonstrates the creation of a link to an anchor in the 'Authors.htm' page using the String.link method.

Properties

hash

Syntax: object.hash

The hash property is a string beginning with a hash (#), that specifies an anchor name in an HTTP URL.

host

Syntax: object.host

The host property is a string comprising of the hostname and host strings.

hostname

Syntax: object.hostname

The hostname property specifies the server name, subdomain and domain name (or IP address) of a URL.

href

Syntax: object.href

The href property is a string specifying the entire URL, and of which all other Link properties are substrings.

pathname

Syntax: object.pathname

The pathname property is a string portion of a URL specifying how a particular resource can be accessed.

port

Syntax: object.port

The port property is a string specifying the communications port that the server uses.

protocol

Syntax: object.protocol

The protocol property is the string at the beginning of a URL, up to and including the first colon (:), which specifies the method of access to the URL.

search

Syntax: object.search

The search property is a string beginning with a question mark that specifies any query information in an HTTP URL.

target

Syntax: object.target

The target property is a string specifying the window that displays the contents of a clicked hyperlink.

text

Syntax: object.text

The text property is a string containing the text of a corresponding 'A' tag.

Methods

handleEvent

Syntax: object.handleEvent(event)

The HandleEvent method invokes the handler for the specified event.

See Also: