Today we discuss about the Print HTML Content using JavaScript.
To print the content page or page with dynamic html.
The document.write() method is used to write data to the tag of your HTML document.
JavaScript code example
var mywindow = window.open('', 'Printing example', 'height=400,width=600'); mywindow.document.write('<html><head><title>Print demo</title>'); /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />'); mywindow.document.write('</head><body>'); mywindow.document.write('<div>I am ready to print</div>'); mywindow.document.write('</body></html>'); mywindow.document.close(); // necessary for IE >= 10 mywindow.focus(); // necessary for IE >= 10 mywindow.resizeTo(screen.width, screen.height); setTimeout(function() { mywindow.print(); mywindow.close(); }, 1000);
CSS apply in print page
.example-print {
display: none;
}
@media print {
.example-screen {
display: none;
}
.example-print {
display: block;
}
}