Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window.print doesn't work in IE

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?

like image 998
Jayyrus Avatar asked Aug 02 '12 16:08

Jayyrus


2 Answers

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());
  });
});
like image 103
mplungjan Avatar answered Nov 08 '22 18:11

mplungjan


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>
like image 43
Andrew Morton Avatar answered Nov 08 '22 20:11

Andrew Morton