Printing the contents of a DIV

Hi

Is there any way to be able to get a function (or something?!) to
print the contents of a DIV without loading the div into it’s own
window etc?

I’ve googled like crazy but seem to be asking the impossible. Any
ideas?

Thanks

Darren

Ruby on Rails: Talk wrote:

Hi

Is there any way to be able to get a function (or something?!) to
print the contents of a DIV without loading the div into it’s own
window etc?
[…]

Most browsers these days understand CSS @media directives, so use a
print stylesheet to turn off display on all other elements.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On Thu, Jun 11, 2009 at 9:23 PM, Ruby on Rails:
Talk[email protected] wrote:

Is there any way to be able to get a function (or something?!) to
print the contents of a DIV without loading the div into it’s own
window etc?

I’ve googled like crazy but seem to be asking the impossible. Any
ideas?

I am not sure I understand what you mean, but just in case I am lucky,
perhaps you are looking for factoring out the DIV into a partial?

On Thu, Jun 11, 2009 at 1:49 PM, Ruby on Rails:
Talk[email protected] wrote:

I’I have a number of DIV’s on a page. One of which contains a calendar
or events. Rather than have to reload a page with just the calendar on
it (to allow for printing), it would seem easier to find a way to
click a button and only the div containing the calendar will print.

If it’s always the same, and you never want to print everything else
on the page, a simple print style sheet would do.


Hassan S. ------------------------ [email protected]

Hi

No :frowning: Sorry.

I’I have a number of DIV’s on a page. One of which contains a calendar
or events. Rather than have to reload a page with just the calendar on
it (to allow for printing), it would seem easier to find a way to
click a button and only the div containing the calendar will print.

I’ve got close to understanding it with the following link:

http://lloydi.com/blog/2006/03/21/how-to-print-selective-sections-of-a-web-page-using-css-and-dom-scripting-2/

But I wonder if there is something easier?

Cheers

Darren

Hi Darren,

On Thu, 2009-06-11 at 12:23 -0700, Ruby on Rails: Talk wrote:

Hi

Is there any way to be able to get a function (or something?!) to
print the contents of a DIV without loading the div into it’s own
window etc?

I’ve googled like crazy but seem to be asking the impossible. Any
ideas?

Google “CSS print stylesheet example”. Hassan and Marnen are correct.
The links you’ll get from the search above will get you to a point where
you understand what they said :wink:

Basically…

In the section of your layout (typically application.rhtml), you
specify the stylesheets you want to use and, optionally, which ‘media’
they’re to be used for. The choices are screen, print, or all. Here’s
an example from one of mine.

<%= stylesheet_link_tag ‘application’, :media => ‘screen’ %>
<%= stylesheet_link_tag ‘print’, :media => ‘print’ %>

These helper methods result in the application.css file being used to
style pages sent to the screen, and the print.css file being used to
style pages sent to the printer.

Put the two lines above in the head section of your layout. Create
an .rhtml view with the following.

I'm your screen buddy!
I'm your print buddy!

Now create the two stylesheets.

application.css

div#for_screen_display {
display:block;
}

div#for_print_display {
display:none;
}

print.css

div#for_screen_display {
display:none;
}

div#for_print_display {
display:print;
}

When the browser loads the page, it notes that you’ve specified
different stylesheets for different media and takes care of the rest.

HTH,
Bill

This sounds excellent!

Just need to work out how to do it.

On Jun 11, 9:59 pm, Hassan S. [email protected]

Bill that was really good information and detailed - many thanks! I was
actually looking for something like this about 3 days ago but put it on
the backburner.

I’m bookmarking the thread for later use.

Thanks.

Hi,

On Fri, 2009-06-12 at 00:42 +0200, Älphä Blüë wrote:

Bill that was really good information and detailed - many thanks! I was
actually looking for something like this about 3 days ago but put it on
the backburner.

I’m bookmarking the thread for later use.

Thanks.

You’re welcome. Glad you found it helpful.

Best regards,
Bill

or

this is stuff I don't want to print.
this is content for screen and print.

print.css

div#for_screen_display {
display:none;

}


this way of someone is using a device that doesn’t support css they
don’t see screen and print content. and less to download :slight_smile:
on a side note there’s no reason to make a div display: block; they
already are.

Cheers,
John