Click to See Complete Forum and Search --> : Print a table


jcongerton
09-29-2003, 05:03 AM
Is there any way I can create a button that prints any given table on the page as apposed to the whole page? Any one know of any scripts or advice would be a great help.

pyro
09-29-2003, 08:06 AM
You could specify a print stylesheet (http://www.alistapart.com/stories/goingtoprint/). Or, if you want to dynamically choose out which table to print, take a look at this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function printTable(obj) {
content = document.getElementById(obj).innerHTML;
newwin = window.open('');
newwin.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"\n',
'"http://www.w3.org/TR/html4/strict.dtd">\n',
'<html>\n',
'<head>\n',
'<title>Printing...</title>\n',
'<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">\n',
'<body>\n',
''+content+'\n',
'</body>\n',
'</html>');
newwin.print();
newwin.close();
}
</script>
</head>
<body>
<p>Print table: <a href="#" onclick="printTable('table0'); return false;">0</a> |
<a href="#" onclick="printTable('table1'); return false;">1</a> |
<a href="#" onclick="printTable('table2'); return false;">2</a></p>
<div id="table0">
<table>
<tr>
<td>Table Zero</td>
</tr>
</table>
</div>
<div id="table1">
<table>
<tr>
<td>Table One</td>
</tr>
</table>
</div>
<div id="table2">
<table>
<tr>
<td>Table Two</td>
</tr>
</table>
</div>
</body>
</html>

jcongerton
09-29-2003, 09:08 AM
I tried the code you gave to me however it does not send anything to the printer? Or give me a print prompt? Sorry i'm very new to javascript.