Code

Add icons to PDF, XLS, DOC file links, links to new windows and more…

Whenever linking on the web, it is important to let the user know exactly where the link leads, especially if opening a new window or the linked item requires a plug-in or external software. For example, if linking to a PDF a small icon can provide a good hint of where the link leads. Unfortunately, manually adding icons to every link through a site can be quite laborious. This is where JavaScript comes in. With few lines of simple code we can automatically add icons to every link on a page.

How this works

For those familiar with JavaScript, the above code is pretty straightforward. But for novice JavaScript authors – such as myself – here is how the function breaks down.

Collect our links and cycle through them

We first collect all the links in a document and places them into array. We then cycle through each one using the following for loop:

We then create a variable for the current link in the array:

Check for linked images

Since we don’t want to be adding these icons to the ends of images, we’ll add a line to collect the name of any images in the link the loop is currently on.

If an image is found, the variable will be defined and the length will be greater than 0, meaning we can filter out any links on images. We also can use indexOf to find out if a file extension is present in the href. Basically, indexOf returns the position of a specific character or set of characters in a string. If nothing is found, then JavaScript returns “-1″. This is useful since we can query the href of our link for a certain character set, in this case file extensions, and if JavaScript does not return “-1″ then a match was found and we can go ahead and append the image.

Adding the file icon

When JavaScript has a match we need to tell it to create a new element – in this case a new image – which we can then append to our link. So, first we create the image, set the source and then add it to the end of the link we are currently parsing.

Adding icons for links that open in new windows is also quite simple, requiring the modification of only one line of code. Instead of using indexOf to check if a particular file extension is used, we use the DOM to check the target. If the target equals “_blank“, indicating a new window will open when clicked, then we also append an image.

Play well with others: Multiple OnLoad Events

To ensure that our function plays well with other scripts that may also be use onload events, we add Simon Wilson’s addLoadEvent function to reduce the chance of conflict.

Areas for Improvement

  1. Since this version of the script adds images to the HTML rather than simply giving the link a new class, it would be slightly more difficult to add additional style declarations than in the earlier example. In this case, since there is no CSS being used, the images all have built-in padding to ensure they aren’t flush with text on the left and right. Adding this padding using a style would probably be more flexible than my current method but would require a little extra code.
  2. Although adding icons can be very useful for users, I generally go one step further and add additional descriptive text noting the file type and size, for example: Link (954 kb PDF), so they can estimate the download time and ensures there is useable message for users who have JavaScript disabled.
  3. This JavaScript will add icons to every link on your page, possibly in places you’d prefer stay icon-free. The solution would be to have the script ignore areas with specific id or class tags, such as: class="no-icons".

Complete Code

About this script

As with all things worth doing, this has been done before. gqGoat’s article, “Automatically Add Icons After Links to PDF Files with the JavaScript & the DOM”, lays the foundation for this script, while I’ve added a few key improvements:

  1. Most importantly, in gqGoat’s method, the icon is added via CSS as a background to the link. For modern browsers this works great, but in Internet Explorer 6 the icon will be aligned poorly for multi-line links and, in many cases, will not even show up.
  2. This method properly uses the DOM instead of relying on innerHTML, which is not standard and not terribly future-proof.
  3. This script includes the onload fix as suggested in gqGoat’s original post by adding Simon Wilson’s “addLoadEvent” function.
  4. Lastly, this adds several additional icons, including those for Word documents, PowerPoint files, and links that open in a new window using target='_blank'.
  • http://www.info.embase.com Karin Krebs

    Hi Darren,

    I was trying to implement this code on our website. I managed to do it, however, I don’t want to have the icons in the footer. Would you be able to explain to me how to disable the icons in the footer?

    You say that the solution would be to have the script ignore areas with specific id or class tags, such as: class=”no-icons”., but I don’t know where and how to add this script (I don’t know much about JavaScript).

    Many thanks in advance for your help.

    Kind regards,

    Karin

  • http://Matthias.tumblr.com Barrett

    I see a lot of interesting posts on your blog.
    You have to spend a lot of time writing, i know how to save you a lot of work, there is a tool that creates unique, google friendly posts in couple of
    seconds, just type in google – k2 unlimited content