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!

printing an invisible page 2

Status
Not open for further replies.

safra

Technical User
Jan 24, 2001
319
NL
Hi,

I have page with a lot of information. A print button on this page will print a defined part of the page. This part is actually an html page with only the contents that needs to be printed.

Is there a way to print this page by clicking the print button but without opening the page in a window? If not, what would be the best cheat trick to simulate this process? I don't want this page to appear on the screen.

Thanks,

Ron
 
If you are using IE 5 (and maybe others, not sure) or higher, you can use the onbeforeprint and onafterprint events of the document to hide the displayed part, unhide the invisible part, print, hide the invisible part, and finally unhide the displayed part. Thanks
John

johnmc@mvmills.com
 
hi John,

Thanks for responding!

Am I right that the window (popup) with the page that needs to be printed will have to open and that there is no alternative to this?

I have been thinking to move the popup window to coordinations outside the screen resolution and then perhaps I can use 'onafterprint' to close the window.

Is there a more appropriate solution for this?

Ron
 
I'm not sure that I am clear on this. Is this it?:

You have a page that you want people to see.

On this page is a button which will print a page that is not visible to the user currently.

You never want the user to see this print page if possible.

If this is the case, you can do this:

Place <div> tags around both pages and put them together like this -

<html>
<head>
<script language=VBScript>
Sub window_onbeforeprint
page1.style.display = &quot;none&quot;
page2.style.display = &quot;block&quot;
End Sub

Sub window_onafterprint
page1.style.display = &quot;block&quot;
page2.style.display = &quot;none&quot;
End Sub
</script>
...
</head>

<div id=page1>
<body>
...
</body>
</div>

<div id=page2 style=&quot;display:none;&quot;>
<body>
...
</body>
</div>
</html>
&quot;A computer scientist is a person who knows when it is time to hit the computer.&quot;

John

johnmc@mvmills.com
 
Didn't know it is possible to put 2 body tags on 1 page?

I tried it and it works but the problem is that the background of 'page 1' is an image and it seems that this image is also positioned on 'page 2'. The background of page 2 should be blank and I don't want the contents of page 1 to be hidden when the print button is clicked?

Another problem is that I only know how to use the print command like this:

window.print()

Probably the contents of 'page 1' will be printed as well when using this code?

What code do I have to use to print 'page 2' and will this solve the problem of the background image in 'page 1'?

Ron
 
Oops, sorry, you should take those <body> tags out of the <divs> and only have one body tag. You can set the <div> of page 1 to have its own background image, and not set one for page 2.

If your 'Print' button on your page calls window.print() then first onbeforeprint fires, then the document is sent to the printer, then onafterprint fires.

The script I put on the last reply will hide page 1 and show page 2 for printing, then hide page 2 and show page 1 immediately afterwards. &quot;A computer scientist is a person who knows when it is time to hit the computer.&quot;

John

johnmc@mvmills.com
 
Great it work, thanks John!

Just to be sure: other browsers than IE will ignore the vb script?

And, anybody knows if IE 4.0 supports the print eventhandlers and which other browsers?

Ron
 
Unless they have a VBScript 'plugin' or whatever. Not sure about other browsers but IE 4 does not support the print events, or the window.print() command. &quot;A computer scientist is a person who knows when it is time to hit the computer.&quot;

John

johnmc@mvmills.com
 
Found this now that the keyword search is back up. This will supposedly print the file &quot;printversion.doc&quot; when the user trys to print. Don't know if it works, but its another possibility.

<link rel=alternate media=print href=&quot;printversion.doc&quot;> &quot;A computer scientist is a person who knows when it is time to hit the computer.&quot;

John

johnmc@mvmills.com
 
Thanks John,

That is exactly what I hoped to be possible!

Unfortunatley it seems this is not supported by NS but neither are the print eventhandlers you first suggested. I guess I will have to deal with that by using a simple popup window.

Thanks a lot for the help,

Ron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top