Home > Code > Javascript > Print a page using JavaScript

Print a page using JavaScript

by Thamil   on Nov 01, 2012   Category: Javascript   |  Views: 1492    |  Points: 25   |  Gold 



Below sample code used to print the content which is inside the div tag in the page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

<script type="text/javascript" language="javascript">
function PrintPage() {
var page_setup = "location=no,toolbar=yes,scrollbars=yes,width=750, height=750, left=50, top=50";
var page_vlue = document.getElementById("page_content").innerHTML;
var docprint = window.open("", "", page_setup);
docprint.document.open();
docprint.document.write("<html>");
docprint.document.write('<body onload="self.print()"><center>');
docprint.document.write(page_vlue);
docprint.document.write("</center></body></html>");
docprint.document.close();
docprint.focus();
}
</script>

</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td width="100%">
<div id="page_content">
<table width="100%" border="0">
<tr>
<td>
test print,test print,test print,test print, test print,test print,test print,test<br />
print, test print,test print,test print,test print, test print,test print,test
<br />
print,test, print, test print,test print,test print,test print,
</td>
</tr>
</table>
</div><br />
<div>
<a href="javascript:PrintPage()">Print this page</a>
</div>
</td>
</tr>
</table>
</form>
</body>
</html>


i hope this sample code will help you to print the required content in the page.




Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts