Unobtrusive Javascript Example - Printing

In lieu of some real posts here’s a quick code sample to keep things ticking along.

I’m sure you’ve had the need to place a nice print button on a web page from time to time. If you haven’t you’re probably alone. A quick google will throw up lots of results for print javascript – the problem is that most of them are horribly obtrusive.

OK, so it’s easy to write something like:

print

But the problem is What if you turn off javascript? The following unobtrusive script automagically adds a print link to the page – in this case as part of a list item which is added to the unordered list with the id of iNav and a class of has-print.

var Print = {
   init: new domFunction(function() {
      if (list = document.getElementById("iNav")) {
         if (list.nodeName.toLowerCase() === 'ul') {
            if (Print.hasClass(list,'has-print')) {
               var item = document.createElement("li");
               item.className = "print";
               var link = document.createElement("a");
               link.href = "#";
               link.onclick = Print.printpage;
               var txt = document.createTextNode("Print");
               link.appendChild(txt);
               item.appendChild(link);
               list.appendChild(item);
            }
         }
      }
   }),

hasClass: function(element,cName) {
return new RegExp(’\\b’+cName+’\\b’).test(element.className);
},

printpage: function() {
window.print();
return false;
}
}

With something like this you want it to load as soon as the dom is ready, rather than on page load. Brothercake’s domFunction comes to the rescue here. The namespacing is based on Stuarts technique which should make it more capable of just being dropped in to any site.

The only problem with this is that it’s just a little longer than the original. I think this is down to one often missed fact, at least until recently – Javascript is a programming language and should be treated as such.

Comments

  1. [...] < [...]

    Morethanseven > JSON configuration example - Printing - 2nd June 2007

  2. Sir,

    I donot want to print the entire page but I like to print the table via Javascript .

    Plz help me .

    An example is given below

    Print The Table only

    Hi
    ee

    Dcode
    Dname
    Location

    D1001
    Comp
    London

    D1002
    IT
    Paris

    Here I want to print the table part only , not the entire document.
    Plz send me a solution of this problem I am facing .

    Thanks in advance .

    With Regards,
    Subhojit

    subhojit - 18th June 2007

  3. I’ve always wondered what the point in having a Print link is. If a user is going to print a page then won’t they use File>>Print?

    Tom Drummond - 20th June 2007

  4. @Subhojit The easiest way of doing this is with CSS. Add a print stylesheet <link rel=“stylesheet” type=“text/css” media=“print” href=“print.css” /> and then use display: none to hide elements you don’t want printing.

    Their is a great article on alistapart (alistapart.com/stories/goingtoprint/)

    @Tom Yes, but a print link is one click and can be made quite big and prominent, encouraging people to print the page. I wouldn’t recomment it for your common or garden website, but what about a web application that produces documents of some kind? It’s all in the user experience, and sometimes as a designer you just want control.

    gareth - 20th June 2007

Comments are now closed.