The Link
object is an indexed array, containing information about all the links in the current document. A link is defined by a <A HREF="...">
construct. Within a document, any text that is the target of a link (i.e. using the <A NAME="...">
construct) should only have an entry in the anchor object, but some text that is both a link and an anchor (i.e. using the <A HREF="..." NAME="...">
construct) would have an entry in both the link and anchor object arrays. Only Netscape fully supports this. Internet Explorer regards any text that is both a link and an anchor as only a link and only an entry in the links array is created.
Also, Netscape makes a Link object entry (and links array entry) for any <AREA>
client side image map hot spot links.
Properties | Methods | Events |
hash, host, hostname, href, length, pathname, port, protocol, search, target | None | OnClick, OnMouseMove**, OnMouseOut*, OnMouseOver |
The Link object can be referenced by referring to the particular links index in the link array. The Link object and links array are properties of the Document Object, so are referenced as :
document.links(index).propertyname
Notice that the Link Object contains more or less the same properties as the Location Object. The main difference, is that for the Link object, the properties are (mostly) read-only. So, for the purposes of a general example for the link object, consider your browser is currently visiting a document, in which the first link (<A HREF="...">
) points to the URL :
http://www.htmlib.com:80/example_files/file.html#findme?Stephen
(again) all will become clear.
Link Properties
hash
The hash
property returns the URL fragment part of an entry in the links array. For example, from the URL above :
vFrag=document.links(0).hash
would set the variable vFrag
to 'findme'.
host
The host
property returns the host section of the link. The host section of a link is basically the hostname
property (see below), together with the port
property (see below). So, using the above example :
vHost=document.links(0).host
would set vHost
to www.htmlib.com:80. Note the subtle difference between this and the hostname
property. (If the link does not contain a port setting, then host
and hostname
are effectively the same).
hostname
The hostname
property (as mentioned above) is the name of the host computer (either a name, or IP address). For example :
vHostName=document.links(0).hostname
sets the vHostNane
variable to www.htmlib.com
href
The href
property can be regarded as a concatenation of all the other link properties. That is, if the link was the one given above for the example, then :
vHref=document.links(0).href
would make vHref
be http://www.htmlib.com:80/example_files/file.html#findme?Stephen
length
The length
property can be used to determine the actual number of links in a document. For example :
vNumLinks=document.links.length
would set vNumLinks
to be the number of links in the current document.
pathname
The pathname
property provides the current path for the link. This is the section in between the hostname
(including protocol
) if any and the hash
, or search
(see below) sections (if either exists). For example :
vPathName=document.links(0).pathname
would return example_files/file.html as the contents of the vPathName
variable.
port
As you may suspect, the port
property represents any port settings for a link. Given the above example,
vPort=document.links(0).port
sets vPort
to 80 (the common port for Web servers).
protocol
This property represents the protocol used for the particular link. For example :
vProtocol=document.links(0).protocol
sets vProtocol
to 'http:'. For commonly used protocols, see the <A HREF="...">
element.
search
The search
property contains information about any search strings in the link. For example :
vSearch=document.links(0).search
returns 'Stephen' as the contents of the vSearch
variable.
target
The target
property can be accessed to determine the target of a link. For example, if the above example was used in the <A HREF="...">
portion of the link, but the link target was set to SearchWindow
(i.e. < A TARGET="SearchWindow">
), then the following :
vTarget=document.links(0).target
would return 'SearchWindow' as the value of the vTarget
variable.
Link Methods
The Link Object has no methods.
Link Events
onClick
The OnClick
event can be used to execute particular code functions when the user clicks on the link. Typically, the event handler would be included within the link and used to execute discrete script functions. For more information, see Scripting the <A> element and Scripting the <AREA> element).
OnMouseMove
The OnMouseMove
event can be used to execute particular code functions when the users mouse moves around the link. For example, it could be used to create an alternative to client side image maps, giving the user more feedback about their destination choices.
NOTE : This event handler is Internet Explorer 3.0 and above specific and is only supported by <A>
links (Internet Explorer doesn't include <AREA>
elements in the links array).
OnMouseOut
The OnMouseOut
event can be used to execute particular code functions when the users mouse leaves the link. Typically, the event handler would be included within the link and used to execute discrete script functions. For more information, see Scripting the <A> element and Scripting the <AREA> element).
NOTE : This event handler is Netscape 3.0 and above specific.
OnMouseOver
The OnMouseOver
event can be used to execute particular code functions when the users mouse enters the link area. Typically, the event handler would be included within the link and used to execute discrete script functions. For more information, see Scripting the <A> element and Scripting the <AREA> element).