【问题标题】:Printing the contents of a URL - javascript打印 URL 的内容 - javascript
【发布时间】:2010-11-08 20:13:09
【问题描述】:

我有一个 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
}

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

有没有办法打印目标网址?如果是,是否可以在不打开新窗口的情况下打印?

感谢您的宝贵时间

【问题讨论】:

  • 打印友好页面已成为过去。使用打印样式表。
  • 如果工作量太大,试试这个: W.onload = function() { print(); }

标签: javascript url printing


【解决方案1】:

尝试使用 CSS @media print

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

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

@media print {
.onprinthide { display: none; }
}

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

【讨论】:

    【解决方案2】:

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

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

    ...除了您的常规样式表。在 print.css 中,将所有内容都设为“显示:无;”除了您要打印的内容。

    【讨论】:

    • 谢谢!。在我的 html 中添加了一个 div 标签,并包含了我不想打印的部分。在我的 print.css 中,将 div 设为“显示:无” - 效果很好
    【解决方案3】:

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

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

    到您的 php 文档,以确保您的文档在打印之前准备就绪,然后让该页面自行打印。

    至于单独从该页面打印,添加一个

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

    应该可以。

    【讨论】:

      猜你喜欢
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多