PDF Links Labeling
Garret Dimon wrote nice post about why .PDF links should be labeled. While there’s no problem making these labels when starting from scratch or re-designing web site, it’s a pain in the a** to add class="pdfLink" to existing (large) sites. Here’s JavaScript to the rescue.
If you have read my post about adding target="_blank” to external links, you are half way there. The script is basically the same, but the conditions are changed. Instead of applying target attribute to all external links, the script applies this attribute to all URLs that have a string ‘.pdf’ in it or as you will see later, to any custom file extension (i know some of you will disagree with opening links in new window, but that’s not today’s topic).
The script also adds class="pdfLink" to all links that match the condition. Same could be applied to .DOC files or .ZIP files.
function fileLinks() {
    var fileLink;
    if (document.getElementsByTagName('a')) {
        for (var i = 0; (fileLink = document.getElementsByTagName('a')[i]); i++) {
            if (fileLink.href.indexOf('.pdf') != -1) {
                fileLink.setAttribute('target', '_blank');
                fileLink.className = 'pdfLink';
            }
            if (fileLink.href.indexOf('.doc') != -1) {
                fileLink.setAttribute('target', '_blank');
                fileLink.className = 'docLink';
            }
            if (fileLink.href.indexOf('.zip') != -1) {
                fileLink.setAttribute('target', '_blank');
                fileLink.className = 'zipLink';
            }
        }
    }
}
window.onload = function() {
    fileLinks();
}
	
 Now that we have classes pdfLink, docLink and zipLink, we can use CSS to style those links, for example add a corresponding icons. See following example:
.pdfLink { padding-right: 19px; background: url(pdf.gif) no-repeat 100% .5em; }
.docLink { padding-right: 19px; background: url(doc.gif) no-repeat 100% .5em; }
.zipLink { padding-right: 17px; background: url(zip.gif) no-repeat 100% .5em; }
	I suggest you use small and unobtrusive, but noticeable icons, otherwise they will take too much attention. Here is full working example. The script is not bullet-proof to URL junk produced by CMS, for example ?download=2387423&sid=8568347563. Links should point to files with the right extensions.

3 Comments
Many people have the same problem. My solution were pop-ups… which was a bad idea.
http://www.individualism.ro/archives/2004/10/polite_links.php
The code is simillar.
Comment (#) by Gabriel Mihalache — 13th January 2005.
Nice. I blogged it:
Ambience - webové ¹tandardy
BTW: Thanks for your postcard, it has finally arrived few days ago. Now it’s posted at my blackboard ;-)
Comment (#) by dusoft — 15th January 2005.
@Gabriel: The beauty is—there is many possible solutions. The bottom line is giving the user proper level of information, so he knows what to expect when he follows a link.
@Dusoft: Glad you receive it finally : )
Comment (#) by marko — 16th January 2005.
Sorry, the comment form is closed at this time, but if you have anything to say, please send me a message.