开发者

PHP stylesheet print switcher problem revisited?

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>


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜