Forum Moderators: open
I'm trying to create a printer-friendly javascript function that will be included on all pages of my site. I have a link on the page that calls a function called showPrintPage(). This is the code for that function:
function showPrintpage(){
document.getElementById("dontPrintTop").style.display = "none";
document.getElementById("dontPrintLeft").style.display = "none";
document.getElementById("copyright_td").style.display = "none";
document.getElementById("relatedlinks_td").style.display = "none";
document.getElementById("printerFriendlyRow").style.display = "none";
document.getElementById("hide").style.display = "inline";
window.print();
}
... on each web page via an include...
<a href="" class="printerFriendlyRow" onclick="showPrintpage();">Printer-friendly page</a>
It hides all the "fluff" navigational things and then prints the page. Right now I'm testing it on Netscape 7.1 and IE 6.0.
It works like I want it to in Netscape 7.1 but in IE 6.0, after some time, it refreshes to the index page in that current directory.
Any ideas on what is causing this?
Also, I'm trying to set the timeout to have a longer delay in refreshing on both browsers but haven't figured out the code for that part yet.
Thanks,
Patricia
This is because when they hit the "printer-friendly" link, it refreshes the page and, using css, hides the areas that I don't want to print.
To have a CSS stylesheet that is always used for printing, just define your stylesheets like so:
<link href="/css/screen.css" rel="stylesheet" media="screen" type="text/css" />
<link href="/css/print.css" rel="stylesheet" media="print" type="text/css" />
No refreshes or extra javascript required. Of course, it brings up the question of user experience because how do they know the page will be made pretty for printing? I like the idea of using javascript to hide the same things the CSS will hide, so why not have your javascript toggle the printing stylesheet to be the main stylesheet? That way, you don't need to change your javascript every time the printing CSS changes. :)
added bold, corrected rel="stylesheet"
<link href="/css/screen.css" rel="stylesheet" media="screen" type="text/css" id="screenCSS" />
<link href="/css/print.css" rel="stylesheet" media="print" type="text/css" id="printCSS" /> And use this javascript to toggle:
function ToggleCSS() {
var screenCSS = document.getElementById('screenCSS');
var printCSS = document.getElementById('printCSS');
if (screenCSS.flag == true) {
screenCSS.href = screenCSS.oldhref;
screenCSS.flag = false;
//alert('When you are done printing, just click the \'Printer Friendly\' link again.');
} else {
screenCSS.oldhref = screenCSS.href;
screenCSS.href = printCSS.href;
screenCSS.flag = true;
}
} And a link like so:
<a href="#" onclick="ToggleCSS();return false;">Printer Friendly</a>