Printing the contents of a div tag a POP-Up window in Javascript

There is a more efficient way.

This JavaScript / jQuery sends parts of the content into a popup for preview and then opens a print dialogue. Print popup window JavaScript and jQuery.

here is the code for that:

function PrintDiv() {    
          var divToPrint = document.getElementById('box_boka');
          var popupWin = window.open("", "_blank", "width=700,height=800");
          popupWin.document.open("", "_blank", "width=700,height=800");
          popupWin.document.writeln(''+stylepage+'' + divToPrint.innerHTML + '');
          popupWin.document.close();
          popupWin.focus();
          popupWin.print();
}

Solved the issue in Opera..

function PrintDiv() {    
          var popupWin = window.open("", "_blank", "width=700,height=800");
          var divToPrint = document.getElementById(\'box_boka\');
          popupWin.document.writeln(\'\'+stylepage+\'\' + divToPrint.innerHTML + \'\');
          if (window.opera) {
            popupWin.onload = popupWin.print;
          } else {
            popupWin.window.print();
          }
          popupWin.document.close();
}

setTimeout for Safari

if (window.opera) {
	popupWin.onload = popupWin.print;
} else {
	var t=setTimeout(function(){popupWin.window.print();},1000);
}

2 comments

  1. Oghenez · · Reply

    how do i add css to the popup?

    1. Please see the line popupWin.document.writeln(\’\’+stylepage+\’\’ + divToPrint.innerHTML + \’\’);
      stylepage variable hold the inline-styles

Leave a comment