views:

36

answers:

1

Okay I have this style sheet switcher which will only works if I leave out the media="print" from the style sheet link.

I was wondering how can I fix this problem without leaving out the media="print" attribute.

Here is the PHP code.

<!-- Print Script -->
<?php if (isset($_GET['css']) && $_GET['css'] == 'print') { ?>
<meta name="robots" content="noindex" />
<link rel="stylesheet" type="text/css" href="http://localhost/styles/print.css" media="print" />
<script type="text/javascript">
//<![CDATA[
if(window.print())
  onload = window.print();
else
  onload = window.print;
//]]>
</script> 
<?php } else { ?>
<link rel="stylesheet" type="text/css" href="http://localhost/styles/style.css" media="screen" />
<?php } ?>
<!-- End Print Script -->

And here is the link you click to change the style sheet.

<a href="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>?css=print" id="print-page" title="Print">Print This Page</a>
+2  A: 

You don't need PHP code for determining whether to output the CSS file for print. By default the browser won't render the "print" stylesheet and should ignore the "screen" stylesheet when printing.

There may be some rendering issues: maybe the browser hasn't had enough time to render the page and feed it correctly to the printer.

A simplified solution would be:

<head>
<link rel="stylesheet" type="text/css" href="http://localhost/styles/print.css" media="print" />
<link rel="stylesheet" type="text/css" href="http://localhost/styles/style.css" media="screen" />
<script type="text/javascript">
function print_it() {
if(window.print())
  onload = window.print();
else
  onload = window.print; 
}
</script>
</head>

<body>
<a href="javascript:print_it();" id="print-page" title="Print">Print This Page</a>
</body>
pygorex1
I would like the user to see the printed page displayed first then the print dialog box.
jab
In that case, on the printer-friendly version of the page, the media for "print.css" should be "print, screen". The browser will show and print using the same stylesheet
Salman A
Also see the comment I made about "if(window.print())" statement
Salman A