When Should I Use Absolute & Relative Hyperlinks?

When Should I Use Absolute & Relative Hyperlinks? thumbnail
Hyperlinks are key to the function of the Web.

When you create a Web page, you can add links to other pages in your own site and to pages on other sites. When you include a link within an HTML Web page that is hosted within the same directory structure as the page you are working on, you can opt to use a relative link rather than an absolute link. This is often helpful when linking to pages within the same site. Relative and absolute links are suited to different contexts.

  1. Context

    • Whether to use a relative or absolute link primarily depends on the context of your page. The location of the page you are linking to, relative to the page you are working on, is what determines which approach you should adopt. An absolute link provides the user's browser with the full Web address of the page you are linking to, so the browser will always be able to resolve the address. A relative link provides only part of the address, indicating the location of the linked page relative to the current page. This means that to construct a relative link as an HTML anchor, you need to work out how to represent this relative location.

    Changes

    • Future changes can affect HTML links. For example, if the location of the page you are linking to changes after you write your code, the link may fail to work, regardless of whether it is relative or absolute. If you alter the internal structure of your website, any relative URLs (Uniform Resource Locators) may become invalid. This includes alterations to the location of the page you are working on and the page you are linking to. Absolute URLs are only affected by changes to the linked page, not to linking pages, whereas relative URLs can be affected by changes within your own site. However, if you move your site to a different location entirely, but its internal structure remains the same, the relative links will remain accurate.

    Code

    • The HTML markup code for absolute and relative hyperlinks is very similar. Both types of link use the anchor tag, with the source attribute indicating the location being linked to. The following sample code demonstrates an HTML link to an absolute URL:
      <a href="http://somesite.com/category/page.html">A Page</a>

      The following code demonstrates a relative link:
      <a href="services/overview.html">Overview of our services</a>

      This reflects a page stored in a directory named "services" which is itself in the same directory as the page listing the anchor. Relative links include enough information for the browser to complete the address based on the location of the current page.

    Choice

    • Choosing relative or absolute URLs is not always easy, as it's impossible to know what's going to happen in the future, even within your own sites. Developers often try to minimize the amount of future maintenance work required for any sites they work on, but it can be unavoidable. If you think the internal structure of your site is going to remain stable, but that there may be a chance you will move the entire site to a different location, it's best to use relative URLs. If you think the site will remain on the same domain, but that its internal structure may change, absolute URLs may minimize the amount of updating required.

Related Searches:

References

Resources

  • Photo Credit Jupiterimages/Comstock/Getty Images

Comments

Related Ads

Featured