Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing the contents of a URL - javascript

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

like image 534
cvic Avatar asked Jul 07 '09 20:07

cvic


People also ask

How do you print the contents of web page in JavaScript?

The print() method prints the contents of the current window. The print() method opens the Print Dialog Box, which lets the user to select preferred printing options.

How do you print the contents of web page?

Open the web page. 2. Press Ctrl + A 3. Right click on the page and left click on “Print” 4.

How do I print the contents of this window using JavaScript?

window. print(): The window object represents a window containing a DOM document; the document property points to the DOM document loaded in that window, window. print() is used to open the Print Dialog to print the current document.


1 Answers

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.

like image 162
Matthew Groves Avatar answered Oct 18 '22 14:10

Matthew Groves