Geeth
Geeth

Reputation: 5343

Window.Print() not working in chrome/Firefox

I am using the following code to print the content in the div. It is woinking fine in IE but it showing blank screen in chrome and firefox.

Code:

var DocumentContainer = document.getElementById('TermsMainDiv');
    var WindowObject = window.open('', "PrintWindow", 
"width=800,height=700,top=200,left=200,toolbars=no,scrollbars=yes,status=no,resizable=no");
    WindowObject.document.writeln(DocumentContainer.innerHTML);
    WindowObject.document.close();
    WindowObject.focus();
    WindowObject.print();
    WindowObject.close();

Upvotes: 1

Views: 8340

Answers (2)

Hans Nouwens
Hans Nouwens

Reputation: 1

This seems to be working too:

WindowObject.document.writeln(html);
WindowObject.document.close();
WindowObject.scrollTo(0,0); 
WindowObject.focus();
WindowObject.print();

Found this when I discovered out that commenting out the .print() function resulted in a correct rendering of the popup page. The scrollTo triggers a rendering normally prevented by the print dialog in front of the page.

Upvotes: 0

Geeth
Geeth

Reputation: 5343

Separate code for IE and other browsers. It is working fine now.

Code:

 <script type='text/javascript'>
 var originalContents;
    function Print() {

        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
            var DocumentContainer = document.getElementById('TermsSCMainDiv');
            var WindowObject = window.open('', "PrintWindow", 
"width=800,height=700,top=200,left=200,toolbars=no,
 scrollbars=yes,status=no,resizable=no");
            WindowObject.document.writeln(DocumentContainer.innerHTML);
            WindowObject.document.close();
            WindowObject.focus();
            WindowObject.print();
            WindowObject.close();
        }
        else {
            originalContents = document.body.innerHTML;
            var printable = document.getElementById('TermsSCMainDiv');
            document.body.innerHTML = printable.innerHTML;
            printCoupon();
        }
    }

    function printCoupon() {
        window.print();
        endPrintCoupon();
    }

    function endPrintCoupon() {
        document.body.innerHTML = originalContents;
        document.getElementById('TermsSCMainDiv').scrollIntoView(true);
        location.reload();
    }
 </script>

Upvotes: 2

Related Questions