Skip to Content, PHP, MySQL, Javascript, Ajax, Htacces

How to print the page using javascript

Be First!
by April 10, 2013 JAVASCRIPT

How to print the page using javascript?

1) Print Window On Load:
using page load we can print easily print the page:
<script type=”text/javascript”>
window.onload = function() { window.print(); }

2) Ater clicking the link also we can print the page:
<a href=”javascript:window.print()”>Print This Page</a>

3) Print Div Content Using JavaScript
instead of printing an entire page we can print a section from the page (based on DIV ID).

<html xmlns="">
<title>Print Page Example</title>
<script language="javascript" type="text/javascript">
function printPage(divID) {
//Get the HTML of div
var divElements = document.getElementById(divID).innerHTML;
//Get the HTML of whole page
var oldPage = document.body.innerHTML;
//Reset the page's HTML with div's HTML only
document.body.innerHTML =
"<html><head><title></title></head><body>" +
divElements + "</body>";
//Print Page
//Restore orignal HTML
document.body.innerHTML = oldPage;

<form id="frmPrint">
<div id="printSection" style="width: 100%; background-color: Green; height: 200px">
Print me
<div id="noprintSection" style="width: 100%; background-color: Yellow; height: 200px">
<input type="button" value="Print First Section" onclick="javascript:printPage('printSection')" />



Leave a Reply