How to open print dialog box to print page or part of the page in JavaScript

some time we need to print only a portion of page or whole page through Javascript, Javascript provide a function named print() to print the current page, so if you want to print whole page then just call window.print() function but if you want to print specific portion of the page then you need to do something extra.

To open print dialog box the current whole page matter just call window.print() function in button click event or link button click event.

How to open print dialog box to print whole page - Javascript


<input type="button" onclick="javascript:window.print()" value="Print Current Page" />


Below is the code, just open a new blank window with the help of window.open and write inner html of the specific portion that you need to print in new window then call print() function for this new window.

How to open print dialog box to print specific part / portion of the page - Javascript


function PrintPartOfPage(dvprintid)
 {
      var prtContent = document.getElementById(dvprintid);
      var WinPrint = window.open('', '', 'letf=100,top=100,width=600,height=600');
      WinPrint.document.write(prtContent.innerHTML);
      WinPrint.document.close();
      WinPrint.focus();
      WinPrint.print();
      //WinPrint.close()   
 }


Calling -

<input type="button" onclick="PrintPartOfPage('divPrnt')" value="Print Portion" />


Popular Posts