7

I have to print out a div which I'm doing in the following way:

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 on IE, when I click the button nothing happens. However, on Chrome and Firefox it works. What can I do to print it out correctly?

EDIT: I'm call print in the following way:

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

This is where print_it is the id of the button.

Another thing I've seen is that after a period of time, Chrome along with other browsers tells me that the page isn't responding. Why is this happening?

Nathangrad
  • 1,426
  • 10
  • 25
Jayyrus
  • 12,961
  • 41
  • 132
  • 214

4 Answers4

8

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

I assume you invoke PrintElem from onclick of something - if that something is a link, you need to return false in 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 am not sure whether or not the window.close() will interfere with the printing

And why not

$(function() {
  $('#print_it').click(function(){
    popup($('#itinerario').html());
  });
});
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 1
    solved... the problem is that i used var mywindow = window.open('', 'a name', 'height=600,width=800'); instead var mywindow = window.open('', '_blank', 'height=600,width=800'); microsoft allows only some argument like name – Jayyrus Aug 02 '12 at 17:55
  • 2
    I already told you that. No space allowed. My code use underscore – mplungjan Aug 02 '12 at 18:42
1

I see you've solved it using mplungjan's note about no spaces in the window name, but an alternative would be to use @media print css styles, crudely:

<!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>
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
1

On my case I just needed to use the code below:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
Luis Sérgio
  • 629
  • 6
  • 8
0

In my case this issue was resolved by doing a focus before the print.

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

Anthony Griggs
  • 1,469
  • 2
  • 17
  • 39