Click here to Skip to main content
Click here to Skip to main content

using javascript create printer-friendly pages.

, 20 Oct 2005
Rate this:
Please Sign up or sign in to vote.
using javascript create printer-friendly pages,Writing the content directly into the popup windows. Using this script the pop up window can be created which holds the data that has to be printed.

Sample screenshot

Introduction

create printer-friendly pages,Writing the content directly into the popup windows.

Using this script the pop up window can be created which holds the data that has to be printed.


 It generates a new page using only content defined between div elements.

 <div class="style3" id="print_content">   </div>

Everything else is ignored.

 

The new page is called with following scripts


<a href="javascript:PrintThisPage()">Cick here to print</a>

With JavaScript we can specify  properties of the window such as width, height, presence or absence of scrollbars, location bar, stauts bar etc.


Properties like

toolbar=0|1   Specifies whether to display the toolbar in the new window. 

location=0|1   Specifies whether to display the address line in the new window. 

directories=0|1 Specifies whether to display the Netscape directory buttons. 

status=0|1   Specifies whether to display the browser status bar. 

menubar=0|1   Specifies whether to display the browser menu bar. 

scrollbars=0|1  Specifies whether the new window should have scrollbars. 

resizable=0|1   Specifies whether the new window is resizable. 

width=pixels   Specifies the width of the new window. 

height=pixels   Specifies the height of the new window. 

top=pixels   Specifies the Y coordinate of the top left corner of the new window. (Not   supported in version 3 browsers.) 

left=pixels   Specifies the X coordinate of the top left corner of the new window. (Not   supported in version 3 browsers.) 


 

<CODE>var  disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,";
 disp_setting+="scrollbars=yes,width=650, height=600, left=100, top=25"

 

Using  innerhtml property to READ the content  between the DIV and /DIV tags.

var content_vlue = document.getElementById("print_content").innerHTML;


The window.open() method opens a new browser window

we must create a new window and store a reference to it in a variable

var docprint=window.open("","",disp_setting);

every window contains a document. We use  docprint.document.write method to write the content to newwindow.
 

 

onLoad attribute inside BODY element executes (javascript onLoad="self.print() )
function and disply the print screen.

docprint.document.write('</head><body onLoad="self.print()"><center>');

docprint.document.close();

at the end is something different than a window.close(), it kind of means that the document inside the popup is closed for writing.we must close the document ourselves. If we didn't close the document, the computer would still be waiting for more HTML, and indeed would parse the next input as HTML, which would generally produce an error.
<CODE>
docprint.focus();
 

It means the focus is on the new pop up window .

 

Complete  Source Code:
======================
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<A href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</A>">
<html xmlns="<A href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</A>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Clinet side printer version</title>
<style type="text/css">
<!--
.style1 {color: #0033FF}
.style3 {
 font-size: 12px;
 font-family: Arial, Helvetica, sans-serif;
}
-->
</style>
</head> 
<script language="javascript">
function Clickheretoprint()
{ 
  var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
      disp_setting+="scrollbars=yes,width=650, height=600, left=100, top=25"; 
  var content_vlue = document.getElementById("print_content").innerHTML; 
  
  var docprint=window.open("","",disp_setting); 
   docprint.document.open(); 
   docprint.document.write('<html><head><title>Inel Power System</title>'); 
   docprint.document.write('</head><body onLoad="self.print()"><center>');          
   docprint.document.write(content_vlue);          
   docprint.document.write('</center></body></html>'); 
   docprint.document.close(); 
   docprint.focus(); 
}
</script>
<body bgcolor="#CCCCCC">
<table width="80%" border="0" align="center" cellpadding="4" cellspacing="4" bgcolor="#FFFFFF">
  <tr>
    <td colspan="2" bgcolor="#CCCCFF"><h2 align="center" class="style1">Site Header Name </h2></td>
  </tr>
  <tr>
    <td width="16%" valign="top" bgcolor="#CCCCFF"> <table width="62%" border="0" align="center" cellpadding="0" 
cellspacing="0">
      <tr>
        <td><a href="#">Link1</a></td>
      </tr>
      <tr>
        <td><a href="#">Link2</a></td>
      </tr>
      <tr>
        <td><a href="#">Link3</a></td>
      </tr>
    </table></td>
    <td width="84%">
    <div align="right"><a href="javascript:Clickheretoprint()"> Click here to print</a>    </div>
    <div class="style3" id="print_content">Content for  id "print_content" Goes Here
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td>Print content Print content Print content Print content Print content Print content Print content Print content 
Print content Print content Print content Print content Print content Print content Print content Print content Print content 
Print content Print content Print content Print content Print content Print content Print content Print content Print content 
Print content Print content Print content Print content Print content Print content Print content Print content Print content 
Print content Print content Print content Print content Print content Print content Print content Print content Print content 
Print content Print content Print content Print<br />
            <br /> 
            content Print content Print content Print content Print content Print content Print content Print content Print 
content Print content Print content Print content Print content Print content Print content Print content Print content Print 
content Print content Print content Print content Print content Print content Print content Print content Print content Print 
content Print content Print content Print content Print content Print content Print content Print content Print content Print 
content Print content Print content Print content Print content Print content Print content Print content Print content Print 
content Print content Print content Print content Print content Print content Print content Print content Print content Print 
content Print content Print content Print content Print content Print content Print content Print content Print content Print 
content Print content Print content Print content Print content Print content Print content Print content </td>
        </tr>
      </table>
    </div></td>
  </tr>
  <tr>
    <td colspan="2" bgcolor="#CCCCFF"><span class="style3">footer</span></td>
  </tr>
</table>
</body>
</html>

 

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

newbegin
Software Developer (Senior) Disney Worldwide Services
United States United States
Rich development experience in Internet/Intranet based applications using Dot net C# , ASPX , ASP, MS SQL Server,AJAX, JavaScript,HTML,XML,CSS.

Comments and Discussions

 
Questionprint in two columns Pinmemberroseannne1414-Dec-12 19:34 
Questioncan not print webcontrol values in an webpage Pinmembergoutamendu27-Jun-12 3:53 
QuestionNot Printing in IE or FF PinmemberTim Knoop26-Mar-12 9:31 
AnswerRe: Not Printing in IE or FF Pinmembernewbegin9-Jul-12 7:57 
QuestionDoesn't work in other browsers PinmemberMojgan Nemati7-Jul-11 7:14 
AnswerRe: Doesn't work in other browsers Pinmembernewbegin25-Jul-11 12:00 
Works fine in firefox 5.0.1.
What version you are using.
new begin
Towards nextgeneration Technology

GeneralSelective printing PinmemberChris Drew16-Apr-09 7:10 
GeneralNice and Easy PinmemberDhirendra Singh Jadon15-Oct-07 18:23 
GeneralNice one PinmemberSiskoTako17-Apr-07 13:42 
Questionprint page number?? Pinmembersiin21-Mar-07 15:56 
QuestionHow to hide the page header and footer PinmemberHarold Espinosa Ch30-Oct-06 13:16 
GeneralThanks Pinmemberbcfusework16-Oct-06 5:08 
Generalprint pop up does not come in this case Pinmemberidreesbadshah20-Sep-06 7:17 
Generalan issue!! PinmemberSanjo19839-Jun-06 1:10 
GeneralRe: an issue!! Pinmembernewbegin2-Oct-06 3:59 
Generalcss?.. Pinmemberkan_izh24-Oct-05 23:43 
GeneralRe: css?.. Pinmembervolkan.ozcelik26-Oct-05 13:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 21 Oct 2005
Article Copyright 2005 by newbegin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid