I am using this jQuery code from Print the contents of a DIV to print the contents of an overlay div:
function PrintElem(elem){
Popup($(elem).html());
}
function Popup(data)
{
var mywindow = window.open('', 'title', 'height=666,width=1000');
mywindow.document.write('<html><head><title>title-title</title>');
/*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
mywindow.document.write('<link rel="stylesheet" href="includes/css/main.css" type="text/css" />');
mywindow.document.write('</head><body >');
mywindow.document.write(data);
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10
mywindow.print();
mywindow.close();
return true;
}
mywindow.document.write('<script type="text/javascript">$(window).load(function() { window.print(); window.close(); });</script>');
</script>
function Popup(data)
{
var mywindow = window.open('', '316steel.com Shopping Cart', 'height=666,width=1000');
mywindow.document.write('<html><head><title>316steel Jewelry Corporation - Shopping Cart</title>');
/*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
mywindow.document.write('<link rel="stylesheet" href="includes/css/main.css" type="text/css" />');
mywindow.document.write('</head><body >');
mywindow.document.write(data);
mywindow.document.write('<script type="text/javascript">$(window).load(function() { window.print(); window.close(); });</script>');
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10
// mywindow.print();
// mywindow.close();
return true;
}
Since the <script></script>
tags are breaking the code, try using this line :
mywindow.document.write('<scr'+'ipt type="text/javascript">$(window).load(function() { window.print(); window.close(); });</scr'+'ipt>')
And without jQuery :
mywindow.document.write('<scr'+'ipt type="text/javascript">function PrintPage() { window.print(); window.close(); } window.onload = PrintPage;</scr'+'ipt>');