Total Pageviews

Thursday, June 17, 2010

Printing a Div in JAVAScript

There several version of implementing. With iframe and without it.

With Iframe

function CallPrint() {

var content = document.getElementById('ctl00_DefaultContent_divShippingRate');
var pri = document.getElementById('ifmcontentstoprint').contentWindow;
pri.document.open();
pri.document.write(content.innerHTML);
pri.document.close();
pri.focus();
pri.print();
}

Add this iframe to html with iframe tags

iframe id="ifmcontentstoprint" style="height: 0px; width: 0px; position: absolute"

Call by button client click

OnClientClick="CallPrint();"

Without Iframe

function CallPrint()
{

var printContent = document.getElementById('ctl00_DefaultContent_divShippingRate');
var windowUrl = 'about:blank';
var uniqueName = new Date();
var windowName = 'Print' + uniqueName.getTime();
var printWindow = window.open(windowUrl, windowName, 'left=0,top=5000,width=7000,height=0');

printWindow.document.write(printContent.innerHTML);
printWindow.document.close();
printWindow.focus();
printWindow.print();

printWindow.close();

}

Just call from onClientclick()

No comments:

Post a Comment