1

I'm new to web development and have a question. It'd like to add multiple links to the same website on my page. Rather than to add the href="www.mywebsite.com" multiple times to the code, is there anyway to add it to one location and reference it?

For example, this is what I have. The button and the text both take to the same website.

<div class="col-md-3 head-main ">             
        <a href="www.mywebsite.com"><i class="fa  color-blue "></i><a>
        <h3>  <a href="www.mywebsite.com" target="_blank">MY WEBSITE</a></h3> 
</div> 
David
  • 11
  • 1
  • You could save the link in a javascript variable then loop over all `a` that you want to reference the link and set their href to your link variable. – Kilmazing Mar 12 '16 at 01:34

1 Answers1

1

If you are linking to the same website as the page is being delivered over, you can use / to refer to the root.

For example: <a href="/">My website</a> when on http://example.com/some/path/page.html will lead to http://example.com/

In addition, there is also the HTML <base> tag that sets the base URL for all relative paths in a page.

<!DOCTYPE html>
<html>
  <head>
    <base href="http://example.com" />
  </head>
  <body>
    <a href=".">Goes to http://example.com</a>
    <a href="/">Also goes to http://example.com</a>
    <a href="#anchor">Goes to http://example.com/#anchor</a>
    <a href="some/path/page.html">Goes to http://example.com/some/path/page.html</a>
    <a href="https://stackoverflow.com">Goes to https://stackoverflow.com</a>
  </body>
</html>

Note that this will apply to all relative links used by the document, including references for scripts and stylesheets, so you must be careful. You may want to look into the pitfalls of <base> before using it.

Community
  • 1
  • 1
nya
  • 46
  • 3