Monday, February 12, 2007

Print the page using Javascript

          Printing the page using javascript is very common functionality that every web developer requires. Every one has implemented that, though I would like to write a blog for this. My requirement is that, I have to hide few controls like buttons, drop downlist etc. before printing the page. To implemet this, I had set the display properties of those controls to "none" before printing and then again reset it. Below is the scrpt for that,

function PrintPage(Control)
{

// Pass the controls ID, which you do not want to print and
// its display property to "none"
Control.style.display = "none"
var PrintValue = document.getElementById('dvPrint').innerHTML;
var WinPrint = window.open('','','left=' + screen.width + ',top=' + screen.height + ',width=1,height=1,toolbar=0,scrollbars=0,status=0');
WinPrint.document.write(PrintValue);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
// Set display property to ""
Control.style.display = "";
return false;
}

No comments: