So you want to create a printer-friendly pages for your website users? Writing the content directly into the popup windows and promting the print command? Want to control how the output looks and feels too? Well you have come to the right place. Lets begin by using this script where the pop up window can be created using javascript which holds the data that has to be printed.
It generates a new page using only content defined between div elements.
code:
<div class="style3" id="print_content"> </div>
Everything else on the page is ignored. The content that appears within the div tags is all that will be displayed. The new page is called with following scripts
code:
<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, resize 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.
code:
var content_vlue = document.getElementById("print_content").innerHTML;
The window.open() method opens a new browser window,so we must create a new window and store a reference to it in a variable as follows...
code:
var docprint=window.open("","",disp_setting);
We use docprint.document.write method to write the content to newwindow.
code:
onLoad attribute inside BODY element executes (javascript onLoad="self.print() )
Then we have to call the function and disply the print screen.
Now at the end we have 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. The following html page is a complete working example for you to work with.
code:
<!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> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Printer Friendly Page Using Javascript</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;