Monday,
8th Sep 2008
Paul

[javascript]

Javascript Text Links

A very simple and easy to maintain way of adding text links to a site is by using a .js file. Below is an example of a javascript text link file:

document.write("<a href=index.cfm>Homepage</a>");

Copy the above line of code into a new file in your HTML editor and save as links.js. Put the file in a subdirectory of your website called 'scripts'. Then type the following code into any page in your web site directory at a point where you wish to call the text links on the page:

<script src="scripts/links.js" type="text/javascript"></script>

Now, when you view your page, you should see a text link saying Homepage at the point you inserted the above code. Now add as many links to the file as you want - that's all there is to it!

HOWEVER...

I know, I know...there's always a catch, right? Well, as in all other areas of web design, there are a few rules you need to follow when creating your JavaScript Text Link file.

» RULE 1

Each row of links must be written on one row in your .js file - CARRIAGE RETURNS CANNOT BE USED.

If you require more than one row of text links, you must finish the first row by inserting a <br> just before the "); at the end of the line. Each new row of links must start with document.write("

If you remember to start all .js files like this: document.write(""); then all you have to do is type your links as normal inbetween the 2 quotes ("")

» RULE 2

Because JavaScript uses quotes as part of it's syntax, you must not put your links inside quotes as you normally would when writing HTML. So, a row of links in a .js file would look something like this:

document.write("<a href=index.cfm>HOMEPAGE</a><a href=javascript:history.back();>BACK</a>");

You may also want to make your links look more attractive by using a character to create a 'break' or applying CSS styles. Below are an assortment of layouts which can all been created using .js files:

Here are the CSS styles used for each example:

/*---js sample1 styles---*/
a.sample1, a.sample1:visited {
  font:normal bold 1.2em/1.4em arial, sans-serif;
  color:#4d92ad;
  text-decoration:none;
  margin:0 5px;
}

a.sample1:hover {
  color:#f58d63;
  text-decoration:underline overline;
}

/*---js sample2 styles---*/
a.sample2, a.sample2:visited {
  font:normal bold 1.4em/1.4em monospace;
  color:#6f5ba2;
  text-decoration:none;
  border-top:1px solid #fff;
  border-bottom:1px dashed #6f5ba2;
  margin:0 5px;
}
a.sample2:hover {
  color:#f58d63;
  text-decoration:none;
  border-top:1px dashed #f58d63;
  border-bottom:1px dashed #f58d63;
}

/*---js sample3 styles---*/
a.sample3, a.sample3:visited {
  font:italic bold 1.4em/1.4em 'times new roman', serif;
  color:#985261;
  text-decoration:none;
  border-bottom:1px dashed #985261;
  margin:0 5px;
}
a.sample3:hover {
  color:#f58d63;
  text-decoration:none;
  border-bottom:1px solid #f58d63;
}

/*---js sample4 styles---*/
a.sample4, a.sample4:visited {
  font:normal normal 1.2em/1.4em arial, sans-serif;
  border:3px double #fff;
  color:#6f5ba2;
  text-decoration: none;
  padding:0 5px 2px;
  margin:0 5px;
}
a.sample4:hover {
  color:#f58d63;
  border:3px double #f58d63;
  text-decoration:none;
}

and here are the scripts used for each one:

You will notice in the JavaScript code, all special characters are referenced using their ASCII code and not simply typed in from the keyboard, i.e., typing &curren; will give you ¤.

Dont forget to replace the href #'s with your own anchor references, i.e. index.htm for the home link or javascript:history.back(); for the back link.

As long as you stick to the basic rules of keeping all your code on one line and not using "" 's around your href text or classes, you should be able to experiment with all kinds of layouts to get a desirable effect on your page - AND THE GREATEST PART IS...when you need to change a link, you only have to change ONE FILE - not every page on your site!