Window.print does not work in IE

I need to print a div , which I do as follows:

 function PrintElem(elem) { Popup(elem.html()); } function Popup(data) { var mywindow = window.open('', 'to print', 'height=600,width=800'); mywindow.document.write('<html><head><title></title>'); mywindow.document.write('<link rel="stylesheet" href="css/mycss.css" type="text/css" />'); mywindow.document.write('</head><body >'); mywindow.document.write(data); mywindow.document.write('</body></html>'); mywindow.print(); mywindow.close(); return true; } 

My problem is that in IE, when I click the button, nothing happens. However, it works in Chrome and Firefox. What can I do to print it correctly?

EDIT: I call print as follows:

 $('#print_it').click(function(){ var element = $('#itinerario'); PrintElem(element); }); 

Here print_it is the button identifier.

Another thing I've seen is that after a while, Chrome, along with other browsers, tells me that the page is not responding. Why is this happening?

+6
javascript internet-explorer printing document
Aug 02 2018-12-12T00:
source share
4 answers

You MUST execute mywindow.document.close() , and you may NOT have a space in the window name

I assume that you are calling PrintElem from onclick of something - if this is something, then you need to return false to the onclick handler!

Here is how I would do it if I had to

 function PrintElem(elem) { Popup($(elem).html()); } function Popup(data) { var mywindow = window.open('', 'to_print', 'height=600,width=800'); var html = '<html><head><title></title>'+ '<link rel="stylesheet" href="css/mycss.css" type="text/css" />'+ '</head><body onload="window.focus(); window.print(); window.close()">'+ data+ '</body></html>'; mywindow.document.write(html); mywindow.document.close(); return true; } 

but I'm not sure if window.close () will interfere with printing

And why not

 $(function() { $('#print_it').click(function(){ popup($('#itinerario').html()); }); }); 
+8
Aug 02 2018-12-12T00:
source share

I see that you solved this using mplungjan, which does not specify spaces in the window name, but an alternative would be to use cssmedia CSS styles roughly:

 <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title></title> <style type="text/css"> @media print { * { visibility: hidden; } .printMe { visibility: visible; } } </style> <script type="text/javascript" src="jquery-1.4.4.js"></script> <script type="text/javascript"> $(document).ready(function () { $("#Button1").click(function () { var divToPrint = $("#div3"); $(divToPrint).addClass("printMe"); window.print(); $(divToPrint).removeClass("printMe"); } ) } ); </script> </head> <body> <div id="div1">fhgjghajghhjagjdag</div> <div id="div2">ytiyrtiyertuyeyiyt</div> <div id="div3">We want to print this one.</div> <div id="div4">vnbcxbvmzxbvc,xbv</div> <div id="buttonContainer"> <input id="Button1" type="button" value="button" /> </div> </body> </html> 
+1
Aug 02 '12 at 18:15
source share

In my case, I just needed to use the following code:

 <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" /> 
+1
Jun 03 '15 at 12:15
source share

In my case, this problem was solved by focusing before printing.

window.focus (); window.print ();

0
Dec 12 '14 at 20:01
source share



All Articles