Friday, May 17, 2024
172
rated 0 times [  174] [ 2]  / answers: 1 / hits: 19137  / 12 Years ago, thu, august 2, 2012, 12:00:00

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?


More From » internet-explorer

 Answers
34

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());
});
});

[#83894] Wednesday, August 1, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dillionsalvadorg

Total Points: 288
Total Questions: 103
Total Answers: 75

Location: South Korea
Member since Sat, Oct 2, 2021
3 Years ago
;