Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-23-2012, 04:54 PM   PM User | #1
Verdaccio
New to the CF scene

 
Join Date: Jan 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Verdaccio is an unknown quantity at this point
Something really simple I hope!

Hello!

I have a script that prints a DIV and I am looking to set the font for the generated page, and the font color and size. But for the life of me, I cannot figure out if it can be done in this script, or if I have to setup a separate print.css.

Here is my script:

Code:
<script type="text/javascript">     
        function PrintDiv_print_all() {    
           var divToPrint = document.getElementById('print_all');
           var popupWin = window.open('', '_blank', 'width=600,height=800,scrollbars=yes');
           popupWin.document.open();
           popupWin.document.write('<html><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
            popupWin.document.close();
                }
     </script>
I am still learning this stuff and any help is appreciated!
Verdaccio is offline   Reply With Quote
Old 01-23-2012, 07:30 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
There are better ways to do this, but...

Code:
popupWin.document.write(
    '<html>\n'
   + '<head><style type="text/css">\n'
   + '* { font-family: courier; font-size: large; color: red; }\n' 
   + '</style></head>\n'
   + '<body onload="window.print()">\n' 
   + divToPrint.innerHTML 
   + '</html>\n'
   );
But of course if there are any inline styles in that <div> they will override the * default.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
Verdaccio (01-23-2012)
Old 01-23-2012, 08:16 PM   PM User | #3
Verdaccio
New to the CF scene

 
Join Date: Jan 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Verdaccio is an unknown quantity at this point
Hello and thanks for your reply and solution!

Yea, I am betting that I could do this more simply if I only knew what I was really really doing around java and CSS.

This started as simply wanting to have a print button on a page to print multiple divs and I ended up with this frankenscript and now I am trying to make it work.

I am happy to hear other ideas and I can only apologize for my patchwork knowledge. I am self-taught and I am learning and growing with each project.
Verdaccio is offline   Reply With Quote
Old 01-23-2012, 08:20 PM   PM User | #4
Verdaccio
New to the CF scene

 
Join Date: Jan 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Verdaccio is an unknown quantity at this point
I thank you for your help!

I am sure there has to be a better way of doing this. This started out with me trying to print multiple divs on a page, and thus I ended up with this frankenscript that now I am trying to make work.

I am open to other suggestions for doing this better, and I apologize for my patchwork knowledge of java and CSS. I am learning as I go!
Verdaccio is offline   Reply With Quote
Old 01-23-2012, 11:43 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Okay, one way to do this:
Code:
<style type="text/css">
@media SCREEN {
    div { display: block; }
}

@media PRINT { 
    div { display: none; }
    div.PRINTME { display: block; font-family: courier; font-size: medium; }
}
</style>
...
<script type="text/javascript">
function printDiv( id )
{
    var pdiv = document.getElementById(id);
    var saveClass = pdiv.className;
    pdiv.className = "PRINTME";
    window.print();
    pdiv.className = saveClass;
}
</script>
You can expand on that theme. As shown, that will only affect div's. If you need it to affect content outside of div's then the CSS needs to be adjusted. But the concept is correct.

Basically, you can use the media choice to completely control the appearance of anything on the page.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:24 PM.


Advertisement
Log in to turn off these ads.