views:

46

answers:

1

A link in an HTML document is normally clickable. However, on a printed page, that feature is by natural causes not available.

<a href="#foo">Link to foo</a>
[... loads of content ...]
<a name="foo"/>Here is foo

Klicking on "Link to foo" on screen will scroll the page to the right place. Is there a way to refer to page number instead if this document is printed? I would like to do like

<a href="#foo" style="printing: page-reference;">Link to foo</a>

to make it print a page number instead of the underlined text "Link to foo".

Is this possible, using HTML 4 or 5, XHTML, CSS 2 or 3, or maybe with help of some fancy javascript?

+4  A: 

CSS allows you to specify different stylesheets for different media types - ie screen and printed page, and also audio, braille and others, as well as different screen/page sizes and resolutions, which makes it very powerful for serving content to mobile devices.

To specify a stylesheet to apply just for print, use @media print. There's a fairly good write-up on it at W3Schools.

In your case, you could use the print media styles along with :before or :after to add additional text to specific elements only when it's printed.

Something like this would do the trick for you:

@media print {
    a[href='#foo'] { text-decoration:none;}
    a[href='#foo']:after { content: "[Ref: 1]";}
    a[name='foo']:before { content: "[1]";}
}

Obviously you can change the text in the content to whatever you like; I've tried to make it close to what you asked for.

The existing content will still be shown (I've suppressed the underline though).

Hope that helps.

Spudley
Whereas you would need some scripting (e.g. Javascript) to have correct dynamic page references, instead of always having "[1]". Besides that I would definitely go with @media print
moontear
Thanks, but if I understand your example correctly, the page number is explicitly defined in the document. I want to refer to the real page number made by the web browser (or the printer driver maybe), so for example if foo happens to be on the fifth printed page, the table of content should refer to page 5.
Johan
@moontear: Can you give an example of such a javascript (using jquery or similar, if needed)? Please post a working example and I will accept your answer.
Johan
Sorry @Johan. You will not be able to find out how many pages your website will actually print on physical paper, because that depends as you said on the printer driver (and the printer being used). The only way to know how many pages are being used is if you insert manual page-breaks ("page-break-after" in CSS). Take a look at A List Apart best practices for @media print http://www.alistapart.com/articles/goingtoprint/
moontear
@Johan - yes, I was setting an explicit ref number in my example. I guess you could think of it as a way of doing footnotes. Its as close as I can come to answering your question. I don't have an answer for finding what page number a reference is on; I really can't think of how to even approach that question. Javascript won't do it. CSS: you might try researching @media a bit more in case I've missed something, but I don't think it'll do what you want. Short of converting to PDF the whole thing to, I can't see an answer.
Spudley
@moontear: Ok, I am sorry to hear that. Not that I am surprised anyway. I was hoping that HTML5 or CSS3 had such a feature though, as it actually should be possible by the web browser to manage that kind of information. If you are sure it is not possible, I will accept that as an answer to this post.
Johan
@Spudley: Yes, I am afraid I'll have to convert it to PDF, but even that would be quite tricky to make page references. Can't think of another way than converting it to a LaTeX document and make a PDF that way. :/
Johan
CSS3 **does** have all sorts of interesting features for printing, but almost no rendering engine supports them. The free FlyingSaucer engine supports some, such as controllable dynamic page headers, and there's an expensive non-free package that supports a lot more, but nothing like that is really useful if you're talking about actual web pages.
Pointy