If you are reading this, please get modern browser.
skip to main content | skip to main navigation | skip to secondary content

PDF Links Labeling

~ 13th January 2005. · 14:16 CET · permanent link · printer friendly ~

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();
}

preview of icons 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

  1. 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.

  2. 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 ;-)

  3. @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 : )

Sorry, the comment form is closed at this time, but if you have anything to say, please send me a message.

* Please keep in mind that this is a personal web site and it does not reflect the position or opinion of my respective employers, organizations or partners.

Typetester – compare screen type Supported by Veer.

What is this?

A web log of Marko Dugonjić, web professional from Croatia. Topics covered:

Translate this site

German, Spanish, Italian, French or Japanese (via).

See you there!

Feel like buying a book?

Try with maratz.com aStore

Worth visiting

top of the page | skip to main content | skip to main navigation | skip to secondary content