Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross Browser Printing using iFrame in Javascript

I want to print using iFrame and javascript. Below are my sample codes for the same:

Javascript

 function printDiv(divP) {

            window.frames["print_frame"].document.body.innerHTML = $(divP).html();
            window.frames["print_frame"].window.focus();
            window.frames["print_frame"].window.print();
        }

HTML

<iframe name="print_frame" width="0" height="0" frameborder="0" src="about:blank">
        </iframe>

This Code is working in IE and Mozilla only. Other Browsers are printing White pages. I don't want to use the Media Queries. What could be the possible issue?

like image 855
Johnny Avatar asked Dec 16 '13 12:12

Johnny


1 Answers

The solution :

Few changes : document.write :( and open and close functions) + iframe 1px size..

 function printDiv(divP) {
  window.frames["print_frame"].document.open();
  window.frames["print_frame"].document.write('<body>aaaaaaaa</body>');
  window.frames["print_frame"].document.close();
  window.frames["print_frame"].focus();
  window.frames["print_frame"].print();
        }

printDiv()

http://jsbin.com/eLIQAXU/4/quiet

this is working in FF , chrome,IE ,safari :

enter image description here

like image 149
Royi Namir Avatar answered Sep 21 '22 20:09

Royi Namir