WordPress – Use javascript to print specific contents in page

275

 

This solution worked for me.

Step 1: Specify the printable contents inside this div.

<a href=’javascript:printContent(“ad_div”)’ id=’print_link’>Print</a>
<div id=’ad_div’>
specify your printable content here…
</div>

 

Step 2: JavaScript function to print contents inside div in your webpage.

function printContent(div_id)
{
var DocumentContainer = document.getElementById(div_id);
var html = ‘<html><head>’+
‘<link href=”css/template.css” rel=”stylesheet” type=”text/css” />’+
‘</head><body style=”background:#ffffff;”>’+
DocumentContainer.innerHTML+
‘</body></html>’;

var WindowObject = window.open(“”, “PrintWindow”,
“width=750,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes”);
WindowObject.document.writeln(html);
WindowObject.document.close();
WindowObject.focus();
document.getElementById(‘print_link’).style.display=’block’;
setTimeout(function() {
WindowObject.print();
WindowObject.close();
}, 2000);
}



Related Articles & Comments

Menu Title
%d bloggers like this: