Fixing sIFR Printing with CSS and MooTools
While I'm not a huge sIFR advocate I can understand its allure. A customer recently asked us to implement sIFR on their website but I ran into a problem: the sIFR headings wouldn't print because they were Flash objects. Here's how to fix the sIFR printing issue.
Sample XHTML
<h2>Sample Heading 1</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ut lectus fermentum augue bibendum tincidunt. In hac habitasse platea dictumst. Nullam ornare nunc ac massa. Nam volutpat tempor tortor. Maecenas sapien. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum a odio ut libero facilisis tincidunt.</p> <h2>Sample Heading 2</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ut lectus fermentum augue bibendum tincidunt. In hac habitasse platea dictumst. Nullam ornare nunc ac massa. Nam volutpat tempor tortor. Maecenas sapien. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum a odio ut libero facilisis tincidunt.</p>
Just some sample XHTML.
The CSS
@media screen { .print-only { display:none; } } @media print { .print-only { display:block; } .no-print { display:none; } }
Setting a couple of standard print-related CSS styles.
The MooTools JavaScript
$$('h2').each(function(el,i) { //new print-only h2 new Element('h2',{ text: el.get('text') }).addClass('print-only').inject(el,'before'); //inject swiff into current h2 el.addClass('no-print'); var swiff = new Swiff('sifr450x23.swf',{ id: 'sifr-' + i, width: 450, height: 23, container: el, params: { wMode: 'transparent' }, vars: { titleText:el.get('text') } }); }); });
We first inject a new H2 element that will be used for print. Then we inject the Swiff into the original H2. Of course you can see where I've added the print-only/no-print CSS classes.
The only browser I found that printed the Flash sIFR objects was Internet Explorer. This solution will work across all browsers.
sIFR itself actually does contain a print stylesheet which hides the Flash movie and shows the alternative text.
I am a little confused too Mark. I have not had a problem using sIFR3 and printing, prints out just fine for me. I know many have the resize and load issues but it is all down to proper use of the styles and setting it up correctly covering everything like line heights etc. Not quite sure Davids Problem here, never run into it not printing before.
My issues were on Firefox 3.1 / Windows XP. The sIFR headings don’t show up which is why I created this script.
david, i’m new to sIFR too and i am looking for a better text-replacement alternative.. did you include the line:
in your heading?
@Marc: Nope, just used the CSS code above.
I didn’t work on FF 3.0.12 for me. Maybe better to use base64 technique for Firefox?
Another great technique as usual. To be honest, your method is far greater than needing all those unnecessary http requests that comes with the original sIFR implementation, CSS & Javascript files.
Having said that, that’s why I prefer using sIFR Lite (By Dave @ AllCrunchy.com) and tweaked your script slightly. Would love to turn it into a Moo-ish class but still don’t know how to do it.
Keep up the great work.