打印URL的内容 - javascript

时间:2009-07-07 20:27:03

标签: javascript url printing

我有一个php页面,其中包含图表,日期选择器(日历)和几个按钮。

我想添加另一个按钮“打印图表”,它只打印图表和图表。不是整个页面 ,在本地打印机中。

我试图通过另一个脚本(只输出图表)并使用javascript函数'window.print'

来做到这一点

HTML

<input type="button" onClick="printChart()"   value="Print Chart">

的javascript

function printChart(){

    var myParameters = window.location.search;// Get the parameters from the current page

    var URL = "http://my_server/my_folder/my_script_that_outputs_only_my_chart.php"+myParameters;

    var W = window.open(URL);

    W.window.print(); // Is this the right syntax ? This prints a blank page and not the above URL
}

我尝试了上面的代码 - 它不起作用。打印空白页。

有没有办法打印目标网址?如果是,是否可以打印而无需打开新窗口?

感谢您的时间

3 个答案:

答案 0 :(得分:5)

您可以使用打印样式表...


<link rel="stylesheet" type="text/css" media="print" href="print.css" />

...除了普通的样式表外。在print.css中,只需将所有内容设为“display:none;”除了你想要打印的内容。

答案 1 :(得分:3)

尝试删除W.window.print()调用并添加

<body onload="window.print();">
...
</body>

到您的php文档,以确保您的文档在打印之前已准备好,并且只需打印该页面。

至于从该页面自行打印,添加

  <input type="button" value="Print" onclick="window.print();" />

应该有用。

答案 2 :(得分:0)

尝试使用CSS @media print

“我想添加另一个按钮”打印图表“,它只能在本地打印机中打印图表而不是整个页面。”

只需尝试javascript打印,然后创建一个类似

的css类
`@media print` {
.onprinthide { dispaly: none; }
}

并适用于所有不需要打印的元素。

相关问题