View Full Version : JavaScript print function

07-04-2006, 01:27 PM
I am relatively new to programming and trying to get a bit of script someone else wrote to work but only part of the script seem to be working and I don't know enough about JavaScript to understand all of the script. Basically what I am trying to do is write a script for a printer friendly page. We have rather elaborate page layouts that do not print very well so I am trying to create a script that only prints out certain elements of a page i.e page title, content, date, url and content heading.

Please can anyone help?

This is the code that we link to the web page template

//This script is used in all templates for the print function
function openTemplatePrintBox() { /* this function opens up a printer friendly version of the page */

pWin = window.open("about:blank", "printWindow", "height=530,width=740,location=no,menubar=yes,status=no,toolbar=no,resizable=yes,scrollbars=yes");
var t = document.forms.fields.PageTitle.value; //this is the same reference used by feedback - it was forms[1].PageTitle
var d = pWin.document;
d.writeln("<html><head><title>" + t + "</title>")
d.writeln("<link href=\"/__802569F4005E3A85.nsf/vWeb/flNDRK6QTEE8/$File/PortalStyleSheetv4.css\" rel=\"stylesheet\" type=\"text/css\"></head>\n");
d.writeln("<body bgcolor=\"white\" text=\"black\" onload=\"javascript:window.print()\">");


This is the code on the template page relating to the ubove script. Also I have to CSS divs called "divPageTitle" and "divContentBox".

<A href="Javascript:openTemplatePrintBox()">Print</a>
<form name=fields>
<input type="hidden" name="DocOwner" value="amanet@baa.com">
<input type="hidden" name="PageTitle" value=" - ">
<input type="hidden" name="SiteName" value="AMA-net">

07-04-2006, 05:53 PM
I would be inclined to look into print style sheets: http://www.alistapart.com/stories/goingtoprint/

It should save you considerable heartache!

Hope this helps


07-09-2006, 04:42 AM
See this Javascript print tutorial.


Hope this helps