Unobtrusive “Print this window” using MooTools and jQuery!
February 3 2009, 12:18am
So I’m a fan of unobtrusive javascript - that means I like avoiding “onclick”, “onblur”, “onfocus” etc. etc. I don’t like doing “href=’[removed]dostuff();return false;’”. It makes me cringe - in this day and age, everyone has their favorite javascript library, and they make event delegation damned easy to the point that I have to ask - if you load a javascript library on your site, why not use it? Print this window! Generally, those “print this window” links tend to be something along the line of:
<a href="#" onclick="window.print();return false;">Print this window</a>
or even:
// ewwww javascript in an href?
<a href="[removed]window.print();return false;">Print this window</a>
Why do it this way? Why create a link that may not work? The MooTools way: MooTools is my rifle - I tend to focus on it first and foremost when it comes to thinking about UI. Let’s take a “Print this Window” link and add a CLASS.
<a href="#" class="print-window">Print this Window</a>
Why CLASS over ID? This is a valid question - some people like having things spelled out down to the letter, and they like their specificity. MY concern here is (recently) a client had a “print” link at the top and bottom of an overlay - IDs become invalid! So I prefer to keep that in mind at all times and code accordingly to the “what-if” scenario:
$$('.print-window').each(function(el){
el.addEvent('click',function(e){
new Event(e).stop(); // if you code your print link as a link, this basically does the same as "return false;"
window.print();
});
});
ZOMG! So easy! And it’ll hit EVERY class with “.print-window”! This method will hit ALL elements, so you may want to tighten it up a little bit:
$('container').getElements('.print-window').each(function(el){
el.addEvent('click',function(e){
new Event(e).stop(); // if you code your print link as a link, this basically does the same as "return false;"
window.print();
});
});
Now, why not search for “a.window-print”? Why use an “anchor” if it’s not going to be used? To be honest - you don’t have to use an anchor. The class name is what’s important - you can make it a <span> if you don’t like using <a> or simply attach it to an image. jQuery “Print this Window”
$('.print-window') .click(function(){window.print();});
Simply delicious! You may need to double check me on that - jQuery is my backup, the dillinger to my glock. What if javascript is not enabled? Gasp! The horror! No doubt this is something that may need to be accounted for - and I’m all about accounting for it all. This also brings up the question - do we care if search engines see it? Are google searches for “print window” a maker or breaker? Really, for our purposes, they’re a “nice to have” function that adds no real added value to our page, and if you don’t care if a search engine sees it, there’s more than one way to jam this tune out. For this, I like to use javascript to inject the elements in the page - javascript is needed to USE the link, so why not use it to make it available? For this, it could be as simple as using CSS to HIDE the links, and javascript to SHOW the element, or you could go a step further and generate the images via javascript to begin with! It’s kind of a pandora’s box - use with care! Copyright © 2009 iKeif - tech and social media geek, mootools fan, and a ton of links. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal@ikeif.net so we can take legal action immediately.Plugin by Taragana
