最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Printing the contents of a URL - javascript - Stack Overflow

programmeradmin4浏览0评论

I have a php page which has a chart, a date picker(calendar) and a few buttons.

I want to add another button "Print Chart" which ONLY prints the chart & not the entire page ,in a local printer.

I am trying to do this by a having another script(which only outputs a chart) and using the javascript function '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
}

I tried the above code - it doesnt work. A blank page gets printed.

Is there a way to print a target URL ? If yes, is it possible to print it without having to open a new window ?

Thanks for your time

I have a php page which has a chart, a date picker(calendar) and a few buttons.

I want to add another button "Print Chart" which ONLY prints the chart & not the entire page ,in a local printer.

I am trying to do this by a having another script(which only outputs a chart) and using the javascript function '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
}

I tried the above code - it doesnt work. A blank page gets printed.

Is there a way to print a target URL ? If yes, is it possible to print it without having to open a new window ?

Thanks for your time

Share Improve this question asked Jul 7, 2009 at 20:27 cviccvic 731 gold badge1 silver badge3 bronze badges 2
  • Printer-friendly pages are a thing of the past. Use a print stylesheet. – Josh Stodola Commented Jul 7, 2009 at 21:23
  • If that's too much work, try this: W.onload = function() { print(); } – Josh Stodola Commented Jul 7, 2009 at 21:24
Add a ment  | 

3 Answers 3

Reset to default 5

You could use a print stylesheet...


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

...in addition to your normal style sheet. In print.css, just make everything "display: none;" except what you want to print.

Try removing the W.window.print() call and adding a

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

to your php document to make sure your document is ready before it prints, and just have that page print itself.

As for printing from that page by itself, adding a

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

should work.

Try using CSS @media print

I want to add another button "Print Chart" which ONLY prints the chart & not the entire page ,in a local printer.

Just try javascript print, and then create a css class like

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

and apply to all of those elements which are not required for printing.

发布评论

评论列表(0)

  1. 暂无评论