PDA

View Full Version : Printing a timestamp on a call to print function



typhoidtimmy
02-05-2008, 05:59 PM
Got a small problem that I am at a loss to figure out how to get it working.

I got a perfectly good call to print function that elminates some surrounding headers/footers to make my data printer friendly....works great.



<script language="JavaScript">
var gAutoPrint = true; // Tells whether to automatically call the print function
function printSpecial()
{
if (document.getElementById != null)
{
var html = '<HTML>\n<HEAD>\n';

if (document.getElementsByTagName != null)
{
var headTags = document.getElementsByTagName("head");
if (headTags.length > 0)
html += headTags[0].innerHTML;
}

html += '\n</HE>\n<BODY>\n';

var printReadyElem = document.getElementById("BotTable");

if (printReadyElem != null)
{
html += printReadyElem.innerHTML;
}
else
{
alert("Could not find the printReady function");
return;
}

html += '\n</BO>\n</HT>';

var printWin = window.open("","printSpecial");
printWin.document.open();
printWin.document.write(html);
printWin.document.close();
if (gAutoPrint)
printWin.print();
}
else
{
alert("The print ready feature is only available if you are using an browser. Please update your browswer.");
}
}
</script>


Except now I need to somehow get a timestamping function to display on the printed page. I cannot have it display on the webpage but can display on the secondary (print preview) page. Is there any way to incorporate a time/date function within my provided javascript to put a small timestamp on the printed page?

BLiZZaRD
02-05-2008, 06:12 PM
Use JS to display the current date/time. A full explanation can be found here (http://www.quirksmode.org/js/introdate.html)

typhoidtimmy
02-05-2008, 08:46 PM
Use JS to display the current date/time. A full explanation can be found here (http://www.quirksmode.org/js/introdate.html)

Yea, well thats fine for displaying it but I do not want to display it on the website....only the final printed page. Attempts to incorporate it into my currect js have proven futile.

You may see it here at the bottom on the "print this page" button.
http://www.saunas.com/Steam%20Shower/Steam%20Rooms/default.aspx

BLiZZaRD
02-06-2008, 05:52 AM
You could use the <link rel type=print option in the head to make it so they print a page different than what they are actually seeing, you can then put the time code on this page.

jscheuer1
02-06-2008, 06:50 AM
You could use the <link rel type=print option in the head to make it so they print a page different than what they are actually seeing, you can then put the time code on this page.

I think you mean media, the type should be text/css for all stylesheet links:


<link rel="stylesheet" href="styles.css" media="screen" type="text/css">
<link rel="stylesheet" href="print.css" media="print" type="text/css">

Once you have separate print and screen stylesheets, you can use them to display only those portions of the document that you wish to for printing, and to format as desired for that purpose. The styles for the live (screen) page would go in the other stylesheet. You could have a division:


<div id="printTime">&nbsp;</div>

In your screen stylesheet, its display property could be none, in the print stylesheet, it could be block. When you go to print, just before the actual print command, do:


document.getElementById('printTime').firstChild.nodeValue=new Date().toLocaleString();

It will place the current date and time in the division, visible only to the printer and print preview.

typhoidtimmy
02-06-2008, 08:09 PM
While I did not go exactly in that direction you suggested, I did figure out a pretty good solution based on your ideas. Thank you both very much!

BLiZZaRD
02-07-2008, 04:18 AM
I think you mean media, the type should be text/css for all stylesheet links:


Yes that is what I meant... it has been a very long week and the Scotch isn't helping.. yet...