Johnny
Johnny

Reputation: 107

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?

Upvotes: 7

Views: 1576

Answers (1)

Royi Namir
Royi Namir

Reputation: 148514

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

Upvotes: 1

Related Questions