Jump to content

HyperText Markup Language/Hyperlinks

50% developed
From Wikibooks, open books for an open world

Hyperlinks are the basis of navigation of the internet. They are used for moving around among sections of the same page, for downloading files, and for jumping to pages on other web servers. Hyperlinks are created with the anchor tag. Let us start with a quick example:

To learn more, see <a href="http://en.wikipedia.org/wiki/Main_Page">Wikipedia</a>.
Will be displayed as: To learn more, see Wikipedia.

Absolute vs. Relative

[edit | edit source]

Before we get into creating a hyperlink (or "link" for short), we need to discuss the difference between an absolute URL and a relative URL. First, the absolute URL can be used to direct the browser to any location. For example, an absolute URL might be:

https://en.wikibooks.org/

However, when there is a need to create links to multiple objects in the same directory tree as the web page, it is a tiring procedure to repeatedly type out the entire URL of each object being linked to. It also requires substantial work should the webpage move to a new location. This is where Relative URLs come in. They point to a path relative to the current directory of the web page. For example:

home.html
./home.html
../home.html

This is a relative URL pointing to a HTML file called home.html which resides in the same directory (folder) as the current web page containing the link. Likewise:

images/top_banner.jpg

This is another relative URL pointing to a subdirectory called images which contains an image file called "top_banner.jpg".

Linking to a Location within a Page

[edit | edit source]

Sometimes specifying a link to a page isn't enough. You might want to link to a specific place within a document. The book analogue of references of this type would be saying "Third paragraph on page 32" as opposed to just saying "page 32". Let's say that you want a link from document a.html to a specific location in a document b.html. Then you start by giving an id to the a particular paragraph in b.html. This is done by adding <p id="some_name"> (where some_name is a string of your choice) as the paragraph tag in b.html. Now that location can be referenced to with <a href="b.html#some_name"> from document a.html.

[edit | edit source]

Now we are ready to create a hyperlink. Here is the basic syntax :

<a href="URL location" target="target">Alias</a>;

In the above syntax, "URL location" is either the absolute or relative path of the object being linked to. "target" is an optional attribute which specifies where the object being linked to is to be opened / displayed. For example :

<a href="https://en.wikibooks.org/" target="_blank">English Wikibooks</a>;

The above example uses an absolute URL of https://en.wikibooks.org/, and specifies a target of "_blank" (which would cause the URL to be opened in a new browser window).

Special Targets

[edit | edit source]
_blank
A new blank window is opened to load the linked document into. The location in the address bar (if shown in the new window) gives the hyperlink location of the new resource requested by the user's clicking on the hyperlink.
_self
The current frame that contains the document and the link to be clicked on is used to load the linked document; if the link is part of a document that occupies a whole window then the new document is loaded into the whole window, but in the case of a frame, the linked document is loaded into the current frame. The location won't be shown in the address bar unless the linked document was loaded into the main window as opposed to a child frame of a frameset.
_parent
The linked document is loaded into the parent frame of the one containing the link to be clicked on; this is only important in nested framesets. If window W contains frameset F consisting of a child frame A and also a child frame B that is itself a frameset FF with "grandchildren" frames C and D (giving us Window W with three visible panes A, C and D), then clicking a hyperlink in the page in frame D with a target=_parent will load the linked document into D's parent frame, that is, into frame B, so replacing frameset FF that was previously defined as the content of frame B. Documents C and D that were the frames of this frameset FF in B will be entirely replaced and this will leave only frame A and the new document from the hyperlink left in frame B, all inside the main frameset F in window W. The location is only shown in the address bar of the window if the parent frame happened to be the window itself.
_top
The linked document is loaded into the window, replacing all files currently displayed in the window in whatever frames they may be found in. The location at the top of the window, in the address/location bar is seen to point to the linked document once the hyperlink is clicked.
[edit | edit source]

An example:

<a href="http://en.wikipedia.org/wiki/HTML">
<img src="http://commons.wikimedia.org/wiki/Image:Html-source-code2.png"></a>

Example rendering:

As you can see, placing hyperlinks on images is in complete analogy to placing them on text. Instead of putting text inside the a element, you place an image there.

[edit | edit source]

To create an email link, use:

<a href="mailto:email@example.com">Email Example.com</a>

.