68

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

Pankaj
  • 723
  • 1
  • 5
  • 6

16 Answers16

139

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...

Ethan Shepherd
  • 569
  • 3
  • 13
Pratik
  • 1,550
  • 1
  • 9
  • 9
  • 5
    Thanks a lot. lack of document.close() was the cause; – Ahmad Mar 26 '13 at 14:35
  • Works pretty well, Chrome identifies this approach as a popup though and blocks it by default – Beta033 May 02 '13 at 15:50
  • 1
    this works for me, thanks. remember to use also the first line, window.document.close(); otherwise it doesent works, for me i mean... – Matteo Bononi 'peorthyr' Sep 03 '13 at 13:59
  • In Chrome 61 the browser instant close the print window. After removing the line `newWin.close()` it workes in chrome well... now the user must close the window manually – Sysix Sep 14 '17 at 07:08
  • @Pratik Hi, thanks for your solution. I've the same problem, but I don't open a new window to print. How do I use close()? Could you please help me? I did open a question for this https://stackoverflow.com/questions/49410902 Thanks – codeispoetry Mar 21 '18 at 16:20
  • I was trying to print with a blob with no luck, any ideas? – gabrielAnzaldo Jun 12 '18 at 20:49
  • `popupWindowRef.document.close();` That is one of the most frustrating things had to diagnose. It took me 3 hours before I gave up and searched and found this Q&A. I should have searched sooner. Aparently, IE11 and FireFox need this, but not Chrome or Edge??? Thanks, though!! – TetraDev Mar 08 '19 at 01:58
12

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

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
Ted
  • 14,757
  • 2
  • 41
  • 58
9
function printDiv() {
    var divToPrint = document.getElementById('printArea');
    newWin= window.open();
    newWin.document.write(divToPrint.innerHTML);
    newWin.location.reload();
    newWin.focus();
    newWin.print();
    newWin.close();
}
sarkiroka
  • 1,485
  • 20
  • 28
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();
    }
Kyle
  • 4,202
  • 1
  • 33
  • 41
  • This reply of yours made almost a year ago has helped me . I have posted this solution w.r.t to my own Qs : http://stackoverflow.com/questions/13948313/how-to-print-a-pdf/13948819#13948819. Up voted . – The Dark Knight Dec 19 '12 at 09:08
3

add checking condition for onload

if (newWinObj.onload) {
    newWinObj.onload = function() {
        newWinObj.print();
        newWinObj.close();
    };
}
else {
    newWinObj.print();
    newWinObj.close();
}
Derin
  • 1,202
  • 1
  • 15
  • 25
3

Just to add some additional information. In IE 11, using merely

window.open() 

causes

window.document

to be undefined. To resolve this, use

window.open( null, '_blank' )

This will also work correctly in Chrome, Firefox and Safari.

I don't have enough reputation to comment, so had to create an answer.

Clinton Chau
  • 557
  • 2
  • 12
3
<!DOCTYPE html>
<html>
<head id="head">
<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE" /> 
  <!-- saved from url=(0023)http://www.contoso.com/ -->
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div>
  <div>
    Do not print
  </div>
  <div id="printable" style="background-color: pink">
    Print this div
  </div>
  <button onClick="printdiv();">Print Div</button>
</div>
</body>
<script>
  function printdiv()
    {
  var printContents = document.getElementById("printable").innerHTML;
  var head = document.getElementById("head").innerHTML;
  //var popupWin = window.open('', '_blank');
  var popupWin = window.open('print.html', 'blank');
  popupWin.document.open();
  popupWin.document.write(''+ '<html>'+'<head>'+head+'</head>'+'<body onload="window.print()">' + '<div id="printable">' + printContents + '</div>'+'</body>'+'</html>');
  popupWin.document.close();
 return false;
};
</script>
</html>
Luyao Chang
  • 31
  • 1
  • 3
3

Just wait some time before closing the window!

if (navigator.appName != 'Microsoft Internet Explorer') {
    newWin.close();
} else {
    window.setTimeout(function() {newWin.close()}, 3000);
}
Greg
  • 31
  • 1
2

I was told to do document.close after document.write, I dont see how or why but this caused my script to wait until I closed the print dialog before it ran my window.close.

var printContent = document.getElementbyId('wrapper').innerHTML;
var disp_setting="toolbar=no,location=no,directories=no,menubar=no, scrollbars=no,width=600, height=825, left=100, top=25"
var printWindow = window.open("","",disp_setting);
printWindow.document.write(printContent);
printWindow.document.close();
printWindow.focus();
printWindow.print();

printWindow.close();
mpaquette
  • 65
  • 6
2

I am not sure but i think it occurs because of the security rules of the InternetExplorer...

If you call a function like print() it asks the user manually if he wants to permit active scripting, if he clicks on the yellow bar and selects 'Yes', the print dialog appears. If you click 'No' or just don't do anything it is not executing the parts which are considered as active scripting or other security relevant javascript functions.

In your example the window is opened then print() is called, confirmation bar pops up (nothing is selected, in fact nothing can be selected due to the short time), newWin.close() is called, window closes.

You should try adding the page to the trusted sites in InternetExplorer or change security settings.

There may be a way of handling the security policies in the javascript itself but i don't know much about InternetExplorer Security Policies.

Hope this helps

Chilln
  • 228
  • 4
  • 15
2

The way we typically handle printing is to just open the new window with everything in it that needs to be sent to the printer. Then we have the user actually click on their browsers Print button.

This has always been acceptable in the past, and it sidesteps the security restrictions that Chilln is talking about.

NotMe
  • 87,343
  • 27
  • 171
  • 245
2

This worked for me, it works in firefox, ie and chrome.

    var content = "This is a test Message";

    var contentHtml = [
        '<div><b>',
        'TestReport',
        '<button style="float:right; margin-right:10px;"',
        'id="printButton" onclick="printDocument()">Print</button></div>',
        content
    ].join('');

    var printWindow = window.open();

    printWindow.document.write('<!DOCTYPE HTML><html><head<title>Reports</title>',
        '<script>function printDocument() {',
        'window.focus();',
        'window.print();',
        'window.close();',
        '}',
        '</script>');

    printWindow.document.write("stylesheet link here");

    printWindow.document.write('</head><body>');
    printWindow.document.write(contentHtml);
    printWindow.document.write('</body>');
    printWindow.document.write('</html>');
    printWindow.document.close();
Nayas Subramanian
  • 2,269
  • 21
  • 28
2

For Firefox use

iframewin.print()

for IE use

iframedocument.execCommand('print', false, null);

see also Unable to print an iframe on IE using JavaScript, prints parent page instead

Community
  • 1
  • 1
2

Close the window only when it is not IE:

function printDiv() {
 var divToPrint = document.getElementById('printArea');
 var newWin= window.open();
 newWin.document.write(divToPrint.innerHTML);
 newWin.print();
 if (navigator.appName != 'Microsoft Internet Explorer') newWin.window.close();
}
kiatng
  • 2,847
  • 32
  • 39
1

I am also facing this problem.

Problem in IE is newWin.document.write(divToPrint.innerHTML);

when we remove this line print function in IE is working. but again problem still exist about the content of page.

You can open the page using window.open, and write the content in that page. then print function will work in IE.This is alternate solution.

Best luck.

@Pratik

Pratik
  • 1,550
  • 1
  • 9
  • 9
0
function functionname() {

    var divToPrint = document.getElementById('divid');
    newWin= window.open();
    newWin.document.write(divToPrint.innerHTML);
    newWin.location.reload();
    newWin.focus();
    newWin.print();
    newWin.close();
}
Immortal
  • 1,233
  • 4
  • 20
  • 47
Venki WAR
  • 1,997
  • 4
  • 25
  • 38