Print the content of a div using JavaScript

Posted on Monday, March 9, 2009 by Krish Tags:
Category: Javascript, Programing

I just wanted to print the content of a div using Javascript. below is the code

function printDiv()
{
  var divToPrint=document.getElementById('DivIdToPrint');
  var newWin=window.open('','Print-Window','width=100,height=100');
  newWin.document.open();
  newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
  newWin.document.close();
  setTimeout(function(){newWin.close();},10);
}

This JavaScript will open a new window with the content of the div, will print it and then will close that window. Last line, this is a must to set a timeout there, Otherwise in IE window will get closed before printing happens.




Post Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011