Forum Moderators: open

Message Too Old, No Replies

printing with Javascript?

Is there built in support for sending something to user's printer?

         

mylungsarempty

3:21 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



This time clock program i'm working on should print the total hours for every employee out on a sheet of paper... Javascript can send data to a printer, can't it?

korkus2000

3:30 pm on Mar 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



window.print();

Brings up the print dialogue.

mylungsarempty

4:35 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



thanks! If i wanted to print what was on just one DIV, could i put window.print(document.nameofdiv) or something like that?

korkus2000

6:38 pm on Mar 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't think you can. The browser print engine is very primitive. Javascript only gives you the one method.

ajkimoto

8:43 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



mylungsaremty,

You can handle this by using one style sheet for screen display and a special style sheet for printing that sets the display property to none for all elements that you do not want to print. If you put the following in the header you will have a link to a normal style sheet and a special one for printing:

<style type="text/css" media="all">@import "mynormal.css";</style>

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

Now, if you had a body with 3 divs like this:

<div id="noprint1">Lorem ipsum</div>
<div id="printme">dolor sit amet</div>
<div id="noprint2">consectetuer adipiscing elit</div>

Your mynormal.css style sheet might contain definitions like:

#noprint1 {color: #ff0000;}
#noprint2 {color: #00ff00;}
#printme {color: #0000ff;}

And your myprint.css style sheet would contain definitions like:

#noprint1 {display: none;}
#noprint2 (display: none;}
#printme {color: #0000ff;}

display:none will hide those divs from the printer.

This solution will work with standards compliant browsers.

ajkimoto