View Full Version : "print this page link"

07-11-2003, 04:08 PM
Hi everyone,

does anyone know of a javascript or php script that will allow clicking on a link to print the current html document in the browser. the link could be text or a small image.



07-11-2003, 04:12 PM
You do not need any script. Browsers have this functionality built-in.

07-11-2003, 04:15 PM
Thanks Vladdy,

what is a browser by the way? never seen one.
and what is a website , any clue?


07-11-2003, 04:34 PM
<a href="javascript:document.print()">Print this page</a>

07-11-2003, 04:46 PM
Thanks NightFire,

I am not conversant with javascript ( cannot write my own codes) . My client wants such a link on his page.

Good to see there are still helpful people like you around.

And there are others who just cannot stand others honesty.
I knew some people would think this is a silly question.

Thanks a ton


07-11-2003, 05:43 PM
Not to be a tool or anything, but I have recently learned that
<a href="javascript:document.print()">Print this page</a> has been depreciated.

The code you REALLY want is
<a href="#" onclick="javascript:window.print();return false">
This makes it so that when the print command is clicked, the screen doesn't reposition itself.

I have learned that there is SO many things that you can do with this code! Like let's say that you don't want the words "Print This Page to appear on the printed document. Just slap this code in the <head> section of your page:

<style type="text/css" media="print">
.dontprintme { display: none; }

And then add on class="dontprintme" after ;return false.
So if you did add all that stuff, your end print code will be
<a href="#" onclick="javascript:window.print();return false" class="dontprintme">

In addition to
<style type="text/css" media="print">
.dontprintme { display: none; }
Pasted into the head of your document. It's that easy. The other way will work, but this is more efficient:thumbsup: .


07-11-2003, 06:08 PM
Thanks Ben
and Nightfire,

there are really so many helpful guys.
Forums are meant to exchange our knowledge and help others, aren't they



07-11-2003, 06:27 PM
Ta Ben for the latest standard :) Been out of js for a few years, so not had the chance to catch up with it all yet :)

07-11-2003, 06:35 PM
Oh, ya...no problem!:o
I just ran into those problems very recently on my page and I had no clue where to even start. I'm a newb at javascript, but I thought that I would share the limited knowledge that I do have. :D


07-12-2003, 12:53 PM
Hi Ben,

Your code is great , I did exactly as you said, it does everything but print the page!

could you please help


07-12-2003, 03:31 PM
use #null to avoid going to the top of the page when the link is clicked.


07-12-2003, 08:51 PM
Moving thread to JS programming...

07-14-2003, 05:21 PM
What browser are you using? IF it is fairly recent IE, then it should work...and I think I know what is going wrong...

The css part that declares
<style type="text/css" media="print">
.dontprintme { display: none; }
tells the browser that whatever has the attribute class="dontprintme" should not be printed...so the only thing i could think of is that you didn't anchor your <a ...>, so just do

<a href="#" onclick="javascript:window.print();return false" class="dontprintme">Print This Page</a>

If this were translated into English, this would mean: Here is a link, but it doesnt go anywhere. Instead when you click me, I want you to print the window, but not return to the top of the page. I also dont want you to print the words that are after the > symbol. However, print everything after the </a>.

Lemme know if that helps!


07-14-2003, 06:46 PM

I tested it on IE 5.2.2 on the Mac that's a fairy recent browser for the mac platform (IE5.3 is the latest)

Will check the codes again and let you know.



08-15-2003, 01:10 AM
Most websites are laid out horizontally in a roughly 3:4 ratio, yet our printers are by default set to print Portrait style.... That doesn't work too well... So I was wondering if there is a way to make....

<a href="#" onclick="javascript:window.print();return false">

print the page in Landscape format, without the end user having to go change anything in their printer properties.

Any help would be appreciated.

Thanks in advance !!

08-15-2003, 02:53 PM
Unfortunately, not seamlessly.

This is due to the lack of support for the @page (media:print specific) selector in CSS parsers in every major browser (at my last check). Were that supported, it would be the preferred way to instruct the printer.

Otherwise, for Windows and I.E. there is ActiveX, with which you can use a system object to move through the menus and popups for the user, to select the landscape option. This can be highly unsecure, and usually not trusted outside an intranet.

If you want specific help with the ActiveX object, PM me and I'll get back to you.