window.print() not working in IE

117,712

Solution 1

Add these lines after newWin.document.write(divToPrint.innerHTML)

newWin.document.close();
newWin.focus();
newWin.print();
newWin.close();

Then print function will work in all browser...

Solution 2

Add newWin.document.close();, like so:

function printDiv() {
   var divToPrint = document.getElementById('printArea');
   var newWin = window.open();
   newWin.document.write(divToPrint.innerHTML);
   newWin.document.close();
   newWin.print();
   newWin.close();
}

This makes IE happy. HTH, -Ted

Solution 3

function printDiv() {
    var divToPrint = document.getElementById('printArea');
    newWin= window.open();
    newWin.document.write(divToPrint.innerHTML);
    newWin.location.reload();
    newWin.focus();
    newWin.print();
    newWin.close();
}

Solution 4

I've had this problem before, and the solution is simply to call window.print() in IE, as opposed to calling print from the window instance:

function printPage(htmlPage)
    {
        var w = window.open("about:blank");
        w.document.write(htmlPage);
        if (navigator.appName == 'Microsoft Internet Explorer') window.print();
        else w.print();
    }

Solution 5

Just wait some time before closing the window!

if (navigator.appName != 'Microsoft Internet Explorer') {
    newWin.close();
} else {
    window.setTimeout(function() {newWin.close()}, 3000);
}
Share:
117,712
Admin
Author by

Admin

Updated on April 08, 2020

Comments

  • Admin
    Admin about 4 years

    I am doing something like this in javascript to print a section of my page on click of a link

    function printDiv() {
     var divToPrint = document.getElementById('printArea');
     var newWin = window.open();
     newWin.document.write(divToPrint.innerHTML);
     newWin.print();
     newWin.close();
    }
    

    It works great in Firefox but not in IE.

    Could someone please help