View Full Version : Print Button Script

03-22-2006, 06:18 PM
This is my first post to a forum ever, so I apologize for any mistakes.

I like the print button script, but I was wondering if it could be modified to:

a. be located anywhere on the page.
b. close the browser after it is pressed.

I am a scripting novice, all self-taught - I know how to parrot, that's about it. I probably want the impossible - please be gentle...

Thanks a bunch


03-23-2006, 07:29 AM
a. ) Already can be anywhere you put it on the page. Just pretend the entire script is an image tag. Place it wherever you want it, in a paragragh or table etc.

b. ) Javascript cannot close a window that javascript did not open. If you open your window using the window.open() method:

<a href="somepage.htm" onclick="window.open(this.href);return false;" target="_blank">Some Page</a>

Then you can close it using the self.close() method:

function printit(){
if (window.print) {
window.print() ;
} else {
var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box WebBrowser1.outerHTML = "";

However this may interfere with the printing. If so, a time out could be used but, even that, in certain circumstances, might not be enough.

Bottom line on this second question is, unless you open the window using the window.open() method and test the live page under low bandwidth situations and the self.close() doesn't halt the print job, don't bother with it.

03-24-2006, 09:11 PM
Thank you soooo much!!:D That was very helpful and you made it so easy to understand. It works great and it did not interfere with the printer.

You had mentioned a time out. I was wondering if it would be possible to make the page that links to this print page "expire" so that it closes and users cannot get back into it to access the print page again.

Grateful for any help:o

03-25-2006, 12:42 AM
Well, once again, the page you want to close must have been opened using the window.open() method as noted above. If so, you can do this various ways. I'd imagine a good way would be to start the countdown when the print page is opened. Put this short script in the head of the page you want to close:

<script type="text/javascript">
function timedclose(){
setTimeout("self.close()", 5000)

Then your link to the print page can look like so:

<a href="printpage.htm" onclick="window.open(this.href);timedclose();return false;" target="_blank">Print Page</a>

The 5000 is the number of milliseconds from when the link is clicked until the page closes (5 seconds in this case).

04-03-2006, 08:56 PM
Cool script - Thanks!!

I gave it a try, but unfortunately I am still stuck with the page before the link being open, which allows users to access the page again by clicking on the link. The user needs to access the print page through a series of previous pages, and I want to make it so they have to go all through those pages to get there.

Is there maybe a way to make it so that a link can only be clicked once?

If all else fails and I need to open a new window each time, is there a way to ensure thay the new window will always pop up in the same place on top of the old window, so at least it looks more uniform?

I know this would be best accomplished through a form, but my server doesn't have the FrontPage extensions required, and I dont know what my other options are (if any) for creating a form and database that do not require server extensions.

Thanks again for all the help

04-04-2006, 04:07 AM
How did you open the page?

04-04-2006, 04:47 PM
The print page is opened by click a text hyperlink on the previous page. I used the window.open() method you gave me and the modified print button script to close the print window, but it leaves the previous window open. This allows the user to simply click the link again to access the print page, avoiding all the previous pages I am trying to require them to see.

This is the scenario I am trying to avoid: User A goes through the pages as required, accesses the print page, prints it, and then leaves the previous windo open. All Users B through ZZZ have to do is click the link to access the print page without ever having seen any of the previous pages. I want there to be no browser windows open once the page is printed.

I want to know if there is a way to make the link on the page previous to the print page clickable only once w/o refreshing. If not, I can use the window.open() with countdown on every page of the series, but, on my screen, the windows that are opening are not fully maximized and they pop up all over the screen. Is there a way to make sure they open in the same position or fully maximized?


04-04-2006, 04:47 PM
The print page is accessed by clicking on a regular text hyperlink. I use the window.open() method. I also used the modified print button script to close the print page. However, the window.open() method opens up a new window and when it closes, it leaves the originating window open, from which the user can just click the link again and open the print page again. I used the window.open() method with the countdown script on the previous page, but just like dominoes it leaves the page before it open and so on, so that the user is now only two clicks away from the print page.

I would like to make it so that once the user has printed the page, he basically has no browser windows open, the effect being that the user will need to open the browser again and start all over once the page is printed. I realize that the user could go into the History files and pull it up again, but I used some naming conventions that will (hopefully) make finding the file more trouble than it is worth than to just go through the pages like I want them to.

I thought the quickest solution would be to make the link clickable only once without refreshing the page, or something like that. If not, I can use the window.open() method with a countdown on every page if I need to, but the windws open in various positions on the screen, and the overall effect is very annoying. I didn't know if there was a way to make sure the window always opens up directly on top of the other when it is not fully maximized, or to force the window to open fully - such is the abysmal depth of my ignorance.

Thanks for bearing with me. As usual any help is wonderful.

04-05-2006, 04:22 AM
This is all rather confusing without being able to see the pages in question. One thing that will help is that when using the window.open() method, if you use a name, and use the same name each time, the new window will simply replace the old window, this may eliminate the need to close so many windows:


Here the window being opened is given the name "printChain". If all windows opened in the series are given the same name, they will replace each other along the way. I'm not sure if the browser's back button would be able to be used to go back through the chain or not. If it is, then using:

<a href="somepage.htm" onclick="self.location.replace(this.href);return false;">Link Text</a>

For each link after the original window.open() method, should do the trick.