打印URL的内容 - javascript

cvi*_*vic 5 javascript printing url

我有一个php页面,它有一个图表,一个日期选择器(日历)和几个按钮.

我想添加另一个按钮"打印图表",它只打印本地打印机中的图表而不是整个页面.

我试图通过另一个脚本(只输出图表)和使用javascript函数'window.print'来做到这一点

HTML

<input type="button" onClick="printChart()"   value="Print Chart">
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢你的时间

Mat*_*ves 5

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


<link rel="stylesheet" type="text/css" media="print" href="print.css" />
Run Code Online (Sandbox Code Playgroud)

...除了正常的样式表.在print.css中,只需将所有内容设为"display:none;" 除了你想要打印的内容.


Ton*_*pel 5

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

<body onload="window.print();">
...
</body>
Run Code Online (Sandbox Code Playgroud)

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

至于从该页面本身打印,添加一个

  <input type="button" value="Print" onclick="window.print();" />
Run Code Online (Sandbox Code Playgroud)

应该管用。