954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do you print part of a page in Javascript?

We are offering a coupon for a free soft drink to everyone who fills out our survey. When the user hits the Submit button at the end of the survey, a page appears with the coupon and a Print button. Currently, the whole page prints, including headings and links. I would like to print only the coupon. This is made trickier by the fact that the coupon is customized for each person with the person's name and the date. All help will be greatly appreciated.

amjones
Newbie Poster
1 post since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

Don't provide the print option on the coupon page. Instead create a 'print view' button at the bottom which will take the user to a page without any frills with just the coupon. I don't know what kind of technique or server side language you are using to persist the user information, but with a bit of hacking, you should be able to easily manage it.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

You can't control functions which belong to the owner of the computer that is viewing your page.

MidiMagic
Nearly a Senior Poster
3,319 posts since Jan 2007
Reputation Points: 730
Solved Threads: 182
 

> You can't control functions which belong to the owner of the
> computer that is viewing your page.
I don't think this is a question of 'controlling the functions of the owner', its rather a question which deals with the selective content which has to be served to the customer, so that the print function prints only the relevant part of the page.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

What I was thinking is that he wanted to do the equivalent of "print selection" with his code making the selection and choosing the "print selection" option in the Print menu.

That belongs to the user.

MidiMagic
Nearly a Senior Poster
3,319 posts since Jan 2007
Reputation Points: 730
Solved Threads: 182
 

I just checked out three coupon pages provided by well-known companies. All of them print the print button on the paper next to the coupon.

MidiMagic
Nearly a Senior Poster
3,319 posts since Jan 2007
Reputation Points: 730
Solved Threads: 182
 

No problem at all, just have the browser open a new page (upon clicking print)

window.open("your page here")
pass the parameters that you need..
i,e. the coupon is customized for each person with the person's name and the date

window.open("yourPage.htm?customername=xxx&date=yyy")

After the page renders with the coupon,
call
document.print() in yourPage.htm
and maybe close the window after the page prints. and voila, one happy customer :)

plasmafire
Newbie Poster
16 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

Better solution: use CSS. With CSS you can create a stylesheet dedicated to printing which is separate from the regular page display. This way you can use CSS to remove the parts of the page you don't want printed and even reformat the parts you do want printed. All major browser support this and it is not turned off as JavaScript often is.

stymiee
He's No Good To Me Dead
Moderator
3,360 posts since May 2006
Reputation Points: 161
Solved Threads: 38
 

As stymiee said... CSS is the best option... example

@media print{
    #div_not_to_print{
        display:none;
    }

    #div_to_print{
        display:inline-block;
    }
}


If every element in your HTML has id or class declare not to print the desired id or class, so when they print the page only print the desire area only, even if JavaScript is turned off. It works with all the browser I used like IE, Safari, Firefox, Chrome, Opera and any Geiko Browser like Firefox.

raphie
Light Poster
44 posts since Jun 2009
Reputation Points: 13
Solved Threads: 4
 

Unfortunately your comment comes almost three years too late for the participants in this thread.

fxm
Posting Pro
596 posts since Apr 2010
Reputation Points: 40
Solved Threads: 74
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You