Important alert: (current site time 7/15/2013 1:44:54 PM EDT)
 

VB icon

Hide all or part of your page when printing

Email
Submitted on: 12/11/2002 12:03:44 AM
By: VBKing 
Level: Beginner
User Rating: By 11 Users
Compatibility: JavaScript
Views: 19654
(About the author)
 
     Allows you to hide all or part of your page when it is printed or previewed. This script is very usefull for hiding navigation menus and other controls, for a cleaner printout.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Hide all or part of your page when printing
' Description:Allows you to hide all or part of your page when it is printed or previewed. This script is very usefull for hiding navigation menus and other controls, for a cleaner printout.
' By: VBKing
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=3324&lngWId=14'for details.'**************************************

<HTML>
<SCRIPT>
<!--
// Hide tags with id="noprint" 
//when printing
function printSetup()
{
var a = document.all.item("noprint");
if (a!=null) {
 if (a.length!=null) {
 //multiple tags found
 for (i=0; i< a.length; i++) {
 a(i).style.display = window.event.type == "beforeprint" ? "none" :"inline";
 }
 } else 
 //only one tag
 a.style.display = window.event.type == "beforeprint" ? "none" :"inline";
}
} 
//-->
</SCRIPT>
<body onbeforeprint="printSetup()" onafterprint="printSetup()">
<p>You can see me and print me <br>
 <!-- set the tag id to "noprint" for elements you want displayed, but not printed -->
 <h2 id="noprint">You can see me, but not print</h2>
 <SPAN id="noprint">
 <br><hr>Works with SPAN and other tags
 </SPAN>
</BODY>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
12/11/2002 4:14:23 PMAgret

hey this is cool, 5/5
(If this comment was disrespectful, please report it.)

 
12/12/2002 11:23:23 AM

Here's a shorter method that uses CSS: Place this once on the page: .printonly { display:none } @media print { .dontprint { display:none } .printonly { display:inline } } The styles in the @media print {}section of the will only apply when printed.
You will only see this when printed.. good for placing messages like "CONFIDENTIAL"
This will not print. Great for navigation bars Works with lots of tags
(If this comment was disrespectful, please report it.)

 
1/17/2003 8:29:19 AMBrad Roberts

Nice. Both of the above. Thanks.
(If this comment was disrespectful, please report it.)

 
2/7/2003 3:38:50 PM

How about controlling the margines of the page?
(If this comment was disrespectful, please report it.)

 
3/17/2003 6:45:20 PM

Nice code, very handy. Problem is, it looks like it only works for IE. Any code that works with Netscape and Opera too?
(If this comment was disrespectful, please report it.)

 
6/14/2003 4:29:05 AM

Its very usefull and I used it as I dont want to show some information to the user and want to be printed only. Show I fired it at window onfocus to hide the content. Thankyou VBKing
(If this comment was disrespectful, please report it.)

 
6/14/2003 4:31:05 AM

Its cool and I used it where I dont want to show some information on page but want to print it. Thankyou VBKing.
(If this comment was disrespectful, please report it.)

 
7/31/2003 2:55:19 PM

This is very helpful to me to hide the images which is shown in the webpages when taking print-out of the page.


(If this comment was disrespectful, please report it.)

 
9/20/2003 11:37:31 AM

Excellent code and css. It helped me a lot.
(If this comment was disrespectful, please report it.)

 
11/20/2003 7:30:04 PMChris'' Web Design

Did the margin question ever get answered? This is great and it works perfect on the site but the margins are not lined up. Half of my table is off the page. Can anyone help?
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.