Printing contents of a div using javascript

Guys this is a nice piece of code which has been tested in IE7 to print the contents of a div. I got this code from http://www.webdeveloper.com/forum/showthread.php?t=152798

While printing keep in mind that on the printed copy you will lose all css styles.
<script type=”text/javascript”><!–
function ClickHereToPrint(){
try{
var oIframe = document.getElementById(‘ifrmPrint’);
var oContent = document.getElementById(‘divToPrint’).innerHTML;
var oDoc = (oIframe.contentWindow || oIframe.contentDocument);
if (oDoc.document) oDoc = oDoc.document;
oDoc.write(“<head><title>title</title>”);
oDoc.write(“</head><body onload=’this.focus(); this.print();’>”);
oDoc.write(oContent + “</body>”);
oDoc.close();
}
catch(e){
self.print();
}
}
// –></script>

<body>

<a onclick=”ClickHereToPrint();”>Print</a>

<iframe id=’ifrmPrint’ src=’#’ style=”width:0px; height:0px;”></iframe>
<div id=”divToPrint”>
content
</div>
</body>

Free books on almost every topic

http://knowfree.net/

You can find free books here on almost every topic.
ajax, php, java, c#, asp, networking training, cisco network, photoshop, graphics, desing, web construction, business ebooks,
SAP, oracle, mysql, mssql, .net, dot net, j2ee, j2me, linux, unix, windows, windows vista, office, database, programming, software development, video training, dreamweaver,
flash, flash training, css, action script, ruby, ruby on rails, python

|