Using a Print Stylesheet for Your Site

April 26, 2005

There are lots of articles on alternate stylesheet switchers and using print stylesheets. Today, a client had an interesting problem: how to get a print stylesheet to load in the browser by putting ?style=print at the end of the URL. if(getQueryParamValue('style') == 'print') { var i, a; for(i=0; (a = document.getElementsByTagName(\"link\")[i]); i++) { if(a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('media') == 'print') { a.removeAttribute('media'); a.setAttribute('media', 'screen'); a.disabled = false; } } } One note: getQueryParamValue() is from FlashObject. I tried every combination of removing the media attribute, setting it to all or screen, etc, and could not get it to work in Mozilla. Opera and IE happily rendered the print stylesheet correctly. I eventually gave up and used document.write() in this case. Yeah, I know, what a cop out.