Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hiding images when printing

Status
Not open for further replies.

Crowley

Technical User
Oct 23, 2002
2
EU
Hi,

Is it possible to display images on screen but not on the printed page? If so, how can I hide the image when the HTML document is sent to the printer?

Thank you so much in advance,
Conor
 
Check out the onBeforePrint and onAfterPrint attributes. The code below will have some elements hidden from the printer, take a look:

<HTML>
<HEAD>
<TITLE></TITLE>
<STYLE TYPE=&quot;text/css&quot;>
.removeforprint
{
font-color=&quot;Navy&quot;
}
</STYLE>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function printHandler()
{
document.styleSheets[0].addRule(&quot;.removeforprint&quot;, &quot;display:none;&quot;);
}

function afterPrint()
{
document.styleSheets[0].addRule(&quot;.removeforprint&quot;, &quot;display:inline;&quot;);
}

window.onafterprint = afterPrint
window.onbeforeprint = printHandler
</SCRIPT>
</HEAD>

<BODY>
Crowley, this stuff will print.<BR>
<DIV CLASS=&quot;removeforprint&quot;>
This stuff will not.<BR>
</DIV>

But it will all be visible on the screen. Give it a try.
</BODY>
</HTML> Kevin
slanek@ssd.fsi.com
 
In IE this is done in the options dialog.

Tools > Internet Options > Advanced > &quot;Printing&quot; category... uncheck the only option in that category.

But that is done by the user visiting your site. You have no control over that.

You could hide all the images and keep your page layout.

This is done with some code similar to this:

[tt]
if(document.images) {
for(var i = 0; i < document.images.length; i++)
document.images.style.visibility = 'hide'; // or hidden
}
[/tt]
I hope this helped! ;-)
- Casey Winans
 
...
sorry i forgot to wrap my code with the proper TGML so the array brackets were interpretted as an italic tag... oops!

[tt]
Code:
if(document.images) {
  for(var i = 0; i < document.images.length; i++)
    document.images[i].style.visibility = 'hide'; // or hidden
}
[/tt]
I hope this helped! ;-)
- Casey Winans
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top