Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.print() not working in IE

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

like image 231
Pankaj Avatar asked Mar 31 '10 18:03

Pankaj


People also ask

Why can I not print from Internet Explorer?

Click the Tools icon in Internet Explorer. Click the Security tab and uncheck the checkbox beside Enable Protected Mode (requires restarting Internet Explorer) Click Apply , and then click OK. Close all open Internet Explorer windows, and then restart Internet Explorer.

How do I enable print options in Internet Explorer?

Print pages by pressing Crtl + P on the keyboard or select the Tools button > Print, and then choose Print. You can also see what the printed page will look like by selecting Print Preview. To print only a picture from a page (and not the whole page), right-click the picture, and then select Print.

Why can I not print from my browser?

The common reasons if the web browsers are not printing could be due to: Conflicts with Add-ons. Incompatible driver Components. Incorrect Printer software settings.

What to do if print preview is not working?

If your Chromebook can't load the print preview, restart your laptop, printer, and router. Additionally, remove and set up the printer again. If the issue persists, clear your Chrome cache, and disable your extensions. You can also reset your browser to default settings.


2 Answers

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

like image 184
Pratik Avatar answered Sep 21 '22 12:09

Pratik


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

like image 24
Ted Avatar answered Sep 18 '22 12:09

Ted