Click here to Skip to main content
11,719,185 members (84,538 online)
Click here to Skip to main content

using javascript create printer-friendly pages.

, 20 Oct 2005 172.7K 2.3K 34
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


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 method opens a new browser window

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


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>');


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.

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=""></A>">
<html xmlns="<A href=""></A>">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Clinet side printer version</title>





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


About the Author

Software Developer (Senior)
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.

You may also be interested in...

Comments and Discussions

Questionprint in two columns Pin
roseannne1414-Dec-12 19:34
memberroseannne1414-Dec-12 19:34 
Questioncan not print webcontrol values in an webpage Pin
goutamendu27-Jun-12 3:53
membergoutamendu27-Jun-12 3:53 
QuestionNot Printing in IE or FF Pin
Tim Knoop26-Mar-12 9:31
memberTim Knoop26-Mar-12 9:31 
AnswerRe: Not Printing in IE or FF Pin
newbegin9-Jul-12 7:57
membernewbegin9-Jul-12 7:57 
QuestionDoesn't work in other browsers Pin
Mojgan Nemati7-Jul-11 7:14
memberMojgan Nemati7-Jul-11 7:14 
AnswerRe: Doesn't work in other browsers Pin
newbegin25-Jul-11 12:00
membernewbegin25-Jul-11 12:00 
GeneralSelective printing Pin
Chris Drew16-Apr-09 7:10
memberChris Drew16-Apr-09 7:10 
GeneralNice and Easy Pin
Dhirendra Singh Jadon15-Oct-07 18:23
memberDhirendra Singh Jadon15-Oct-07 18:23 
GeneralNice one Pin
SiskoTako17-Apr-07 13:42
memberSiskoTako17-Apr-07 13:42 
Questionprint page number?? Pin
siin21-Mar-07 15:56
membersiin21-Mar-07 15:56 
QuestionHow to hide the page header and footer Pin
Harold Espinosa Ch30-Oct-06 13:16
memberHarold Espinosa Ch30-Oct-06 13:16 
GeneralThanks Pin
bcfusework16-Oct-06 5:08
memberbcfusework16-Oct-06 5:08 
Generalprint pop up does not come in this case Pin
idreesbadshah20-Sep-06 7:17
memberidreesbadshah20-Sep-06 7:17 
Generalan issue!! Pin
Sanjo19839-Jun-06 1:10
memberSanjo19839-Jun-06 1:10 
GeneralRe: an issue!! Pin
newbegin2-Oct-06 3:59
membernewbegin2-Oct-06 3:59 
Generalcss?.. Pin
kan_izh24-Oct-05 23:43
memberkan_izh24-Oct-05 23:43 
GeneralRe: css?.. Pin
volkan.ozcelik26-Oct-05 13:41
membervolkan.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 | Terms of Use | Mobile
Web02 | 2.8.150901.1 | Last Updated 21 Oct 2005
Article Copyright 2005 by newbegin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid