MCCC CIS 177 - Markup Languages

3.0 Links

The Anchor <a> tag

2.15 External links

A text hyperlink is a section of text that is specially tagged as a link. Links are what makes the Web because thay connect pages to other files across the Internet. To create a link you use the <a>, or anchor tag with the href attribute that specifies the location of the linked page or other file.

<a href="filename.html">Click here!</a>

2.23 Absolute path names

Use absolute links to link to pages located on other servers. In an absolute link, you specify the fully qualified URL in the link. While you can use an absolute link within your own site, it is not recommended because it places an additional unecessary burden on the server, which must resolve the domain name before it can retrieve the page.

<a href="http://www.wccnet.org">WCC</a>

appears on the page as:

WCC

Create a Linked Web Page

Create an HTML document
Create a link to MCCC's server

http://www.monroeccc.edu/

Save the file
Test the link in the browser

2.24 Relative Paths

Relative links can only be used to link to files located on the same server as the page containing the link. You must be careful to specify the correct directory level. Use the double dot (..) to indicate moving up one level in the directory structure.

Common Directory Structure

 folder01

 folder01a
 folder01b
 folder01c

 folder02
 folder03

 folder03a
 folder03b

 folder03b_1
 folder03b_2

 

For example, assume all the folders contain an index.html page:

  1. A link from folder01 to folder03:
  2. <a href="../folder03/">Folder 3</a>

  3. A link from folder03b to folder01:
  4. <a href="../../folder01/">Folder 1</a>

  5. A link from folder01c to folder03b_2:
  6. <a href="../../folder03/folder03b/folder03b_2/">Folder 3</a>

  7. A link from folder01c to folder01b:
  8. <a href="../folder01b/">Folder 3</a>

Trailing slashes

While you do not have to include the trailing forward slash, it relieves the server of some work because the slash tells the browser to look for a default page. Without the slash, the browser first looks for a file named the same as the last folder specified and when it doesn't find one, looks for a default page. Although the above assumes a link to the default page contained within each folder, if you want to link to a page with a different name, simply specify the file name in the link following the trailing forward slash.

Targeting links

By default, a link opens in the same browser window containing the link. To control which window a link opens in, you can use the target attribute in your anchor tag. The following are the HTML built in targets.

The correct syntax for targeting links is as follows. Simply replace _blank with your choice of targets as described above:

<a href="linkurl" target="_blank">

  • _self : the same as the default, the link opens in the window containing the link
  • _blank : the link opens in a new window
  • _top : the link opens in the same window containing the link, however, if the source is within a frame, the link replaces all of the frames (more about this in lesson 6)
  • _parent : the same as _top
  • windowname : specify a window name
  • framename : specify a frame name

You can change the default target for all links on your page by using the <base> tag in the <head> section of your page:

<base target="name">

Use the target attribute

Using the document created in the previous demo, experiment with the target attribute

Be sure to save the document after each change

Test the link using various targets in the browser