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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print frame

Status
Not open for further replies.

bluebytez

Programmer
Dec 20, 2001
39
MY
I would like to use window.print() to print the contents of a page but I do not want to print the "Print" button on the same page.
Is there any way I can get around this ?
Thanks !
 
Hi

One solution is to open a printer friendly version of the page in a new window. That means a page without those objects you don't want to print (buttons etc.)

Use JavaScript to check if the print button was clicked. If so then open the same page (without buttons)in a new window, print the contents and then close the window after a few seconds delay.

rgds Bjorn
....................................

<SCRIPT>
//------ Print active Window
function printWindow() {
bV = parseInt(navigator.appVersion);
if (bV >= 4) window.print();
}

//------ Delay window closing
function delayCloseWin(dSecs) {
setTimeout('self.close()',dSecs);
return true;
}
</SCRIPT>


Contents to be printed

<SCRIPT>
printWindow();
delayCloseWin(5000);
</SCRIPT>
 
thanks for the advice bjorn, but what if the new window is printed BEFORE the page has finished loading, might be due to slow connection or heavy page.
anyway, i appreciate your help :)
 
Ok. Here is another solution with use of more code. You'll find the complete code below. This is working for MIE 5.5. This time you are uing one window only.

Feel free to use it.

Bjorn

-------------------------

<HTML>
<HEAD>
<script language=&quot;JavaScript1.2&quot;>
if (window != top) self.parent.location = location.href;

var str = &quot;<TABLE cellSpacing=\&quot;0\&quot; cellPadding=\&quot;0\&quot; border=\&quot;0\&quot; width=\&quot;100%\&quot;>&quot; +
&quot;<TR><TD colspan=\&quot;2\&quot;>&nbsp;</TD></TR><TR><TD><a href=\&quot;javascript:void(printPage());\&quot;><img src=\&quot;btn-print-page.gif\&quot; alt=\&quot;Click Here to Print\&quot; width=\&quot;115\&quot; height=\&quot;20\&quot; border=\&quot;0\&quot;></A></TD>&quot; +
&quot;<TD class=\&quot;font-cn\&quot; align=\&quot;right\&quot;><table cellSpacing=\&quot;0\&quot; cellPadding=\&quot;0\&quot; border=\&quot;0\&quot;>&quot; +
&quot;<td><img src=\&quot;btn-print-page.gif\&quot; width=\&quot;2\&quot; height=\&quot;19\&quot;></td><td class=\&quot;font-cn\&quot; bgcolor=\&quot;#E6E6E6\&quot; align=\&quot;right\&quot; nowrap>&quot; +
&quot;&nbsp; <A class=\&quot;ptnavbar\&quot; href=\&quot;javascript:self.close();\&quot;>Close</A>&nbsp;</td></tr></table></td></tr><tr><td></td></tr></table>&quot;;
</script>

<script language=&quot;JavaScript1.2&quot;>
var agt=navigator.userAgent.toLowerCase();

function printPage() {
var prn = new Image();
prn.src=&quot;&quot;;

if (window.print) {
setTimeout('window.print();',200);
}
else {
alert(&quot;Press 'Ctrl+p' on your keyboard to print page.&quot;)
}
}

function hidePrint() {
if (document.all) {
document.all.hideTop.innerHTML = str;
document.all.hideBottom.innerHTML = str;
}
}

function showPrint() {
if (document.all) {
document.all.hideTop.innerHTML = &quot;&quot;;
document.all.hideBottom.innerHTML = &quot;&quot;;
}
}

</script>
<title>Print</title>

</HEAD>
<BODY bgColor=&quot;#FFFFFF&quot; onLoad=&quot;hidePrint();&quot; onbeforeprint=&quot;hidePrint();&quot; onafterprint=&quot;showPrint();&quot; onload=&quot;focus();&quot; onfocus=&quot;hidePrint();&quot;>

<div id=&quot;hideTop&quot;>
</DIV>


<!--Text to Print goes here-->
<DIV>
<TABLE cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot; width=&quot;100%&quot;>
<tr>
<td>

This is the only text to be printet

</td>
</tr>
</table>
</DIV>
<!--Text to Print End-->


<DIV id=&quot;hideBottom&quot;>
</DIV>


</BODY>
<script language=&quot;Javascript1.2&quot;>
setTimeout('showLayer();',200);
</script>
</HTML>
 
PS:
I forgot to mention; You may have to remove the onfocus statement in the body tag. Otherwise a delay function has to be added.

Hopefully you can use the code as a baseline to build your own functionality on.

Bjorn
 
hmmmmmm. you can do this with stylesheets for compliant browsers (ie5 and above and ns6 i think). all you have to do is specify the styles to be applied at the print stage using the media attribute like this

<link rel=&quot;stylesheet&quot; href=&quot;s.css&quot; media=&quot;print&quot;>

or

<style type=&quot;text/css&quot; media=&quot;print&quot;></style>

or

actually in your stylesheet like this

@media print{
.noprint{display:none}
}

all i have done is set display:none for a class noprint in the print only section of the stylesheet. apply ther class noprint to anything you dont want to appear

hope this helps

rob
 
thanks to rob, i went and did more research on using css to control printing and here's what i found :
there is actually some style-sheet commands to break a page before printing.

page-break-after
page-break-before

You can pretty much pick out what each does. The first sets the printing page break just before the element, the second sets the page break just after.

Each command has four attributes:

always | auto | left | right

always tells the browser to break the print page after this element all the time.

auto is the default. You're telling the browser to do what it would do anyway: Page break where the page ends.

left and right is not supported by any browser yet. It is used if your printer will print both sides of a page, like a manuscript.

what i can do is put
<style type=&quot;text/css&quot;> Pr.breakhere = {page-break-before: always} </style> in my <head> section
and use the Pr class whenever i want to break the page.
<Pr class = &quot;breakhere&quot;>

just thought u guys might be interested to know, thanks for the help anyway !
 
continuing from my previous post, actually rob's method is better. page-break-before only breaks the page into two pages, so I still get the print button but only in another page. I've tried rob's method and it works great.
Thanks, rob !
Now, I wonder if there is a way to force it to print on landscape mode.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top