print div content or web page using javascript


The following code can you help to print div content.
<div>Print div content</div>
<div id="divPrint" style="height:500px ; width:600px">
   Name: Dilip Singh<br />
   Address: Delhi<br />
   Roll Id: DL1100
   </div>

   <asp:LinkButton ID="lnkPrint" runat="server"
OnClientClick="printDiv('divPrint')">Print</asp:LinkButton>

   <script type="text/javascript">
       function printDiv(divId) {
           var printContents = document.getElementById(divId).innerHTML;
           var originalContents = document.body.innerHTML;
           document.body.innerHTML = "<html><head><title></title></head><body>" + printContents + "</body>";
           window.print();
           document.body.innerHTML = originalContents;
       }


</script>

Note:* If you want to print your hole web page use following script code.


<script type="text/javascript">
       function printPage() {
           
           window.print();
          
       }
</script>

No comments:

Post a Comment

Please do not enter any spam link in the comment box.

Related Posts

What is the Use of isNaN Function in JavaScript? A Comprehensive Explanation for Effective Input Validation

In the world of JavaScript, input validation is a critical aspect of ensuring that user-provided data is processed correctly. One indispensa...