Welcome to WebmasterWorld Guest from 54.198.134.104
Forum Moderators: open
However I thought it would be cool if I could provide the text in a form text field and print just the text (using a print button).
Can anyone point me in the right direction?
1.open a popup window
2. write there the value of a desired text box
3. open Print dialog automatically
Here's an example similar to the one I used once:
function printText(elem) {
popup = window.open('','popup','toolbar=no,menubar=no,width=200,height=150');
popup.document.open();
popup.document.write("<html><head></head><body onload='print()'>");
popup.document.write(elem);
popup.document.write("</body></html>");
popup.document.close();
}
Then you should call it like this:
<form name=f1>
. . .
<input type=text name=t1>
<input type=button value="print it" onclick="printText(document.f1.t1.value)">
<textarea name=t2></textarea>
<input type=button value="print it" onclick="printText(document.f1.t2.value)">
Very simple and effective.
You can also print the entire form by looping through it's elements.
good luck
In a previous revision of the site I am updating, I used a link to a 'printer friendly' page, but I wanted something that jumped out of the page to say 'hey print/download me!'. I think I may cause myself more trouble than its worth, but thanks to your help, I now have a few more options!
All your elements you don't want printed you would give a class -- say, "noprint". You then create a simple (external) stylesheet with this rule:
.noprint { display: none; }
Save it as (say) "printstyles.css". Import it into the document with the following directive:
@import url(printstyles.css) print;
You can, similarly do the reverse: have the printer print a message that doesn't appear on the screen. Or specify a different font (san-serif fonts are easier to read on screen, serif fonts are easier to read on paper). And so on.
It works in all browsers that understand print() function:
IE5+ (can't test in IE4), NN4.x, Mozilla/N6, Opera 6 (unfortunately, it wans't available before) on PC,
and IE5 and NN4.x on Mac (these were the only ones where it was tested).