Click to See Complete Forum and Search --> : Print Table Help


jcongerton
09-29-2003, 10:45 AM
Hi All

Pyro was good enough to give me some code and am now nearly acheiving what i set out to do, however there is a slight problem. I am trying to print only the table i choose on the page, at the moment the contents of the table are written to a new document, the document should then open in a new window, call the print dialog, and print the page, however at the moment the print dialog will not engage. The code is below. Thank you.

<!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>

pyro
09-29-2003, 11:08 AM
It works fine in Mozilla. You might want to remove the newwin.close(); line, so IE users can manually print the window.

ggogggo
12-20-2004, 12:53 PM
hi, i used teh code you've given and modified it, so my IE can print it
actully, i just added

..

'<a href="javascript:window.print()">IE users - Print</a> \n',
'<br/></br>'+content+'\n',
'<br/><a href="javascript:window.close()">Close Window</a>\n',

in your printTable function
As you might not expect, it DOESN'T work in IE ( it works in mozilla, though;>)
if i put this line
<a href="javascript:window.print()">IE users - Print</a>
in a 'regular' html document it DOES work..
do you have any idea why is this ? (other than that IE is SUCH a browser)
thanks, appreciate teh efforts of people like you

peace

rjvibes
11-06-2007, 10:49 PM
Hi ggogggo,
Its urgent can you send me an html file which you have modified for printing tables in an html page which works both on mozilla and ie as well. My page works fine with mozilla. I am not a programmer but a designer, but if you give me the codes I could insert it wherever applicable. Waiting to hear from you soon

Regards
mail me at romy.vibes5@gmail.com

cleake
12-16-2010, 07:46 AM
Without the highlighted lines this will work in FireFox but fail in IE.

<html>
<body>

<script type="text/javascript">
newwin = window.open('','','width=200,height=100');
newwin.document.write("Hello World!");
newwin.document.close();
newwin.focus();newwin.print();
newwin.close();
</script>

</body>
</html>