Results 1 to 5 of 5

Thread: Javascript code when Print Previewing?

  1. #1
    Join Date
    Feb 2010
    Posts
    66
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Exclamation Javascript code when Print Previewing?

    Hi Guys,

    Is there such a code that when you click on Print Preview, that Div which contains text, uses another Print Sheet?

    I know you have that media="screen" code but is there another way of avoiding having two divs (One for print preview which is basic).

    Thanks,

    Mark

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Here's one that I use ;

    First put this script in the <head>;
    Code:
    <script type="text/javascript"> 
    function PrintPage()
    	{ 
    	var display_setting="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
    	display_setting+="scrollbars=yes,width=800,height=600,left=100,top=25,toolbar=0,location=0,statusbar=0,menubar=0,resizable=yes,titlebar=yes"; 
    	var content_body = document.getElementById("print_content").innerHTML; 
    	var docprint=window.open("","",display_setting); 
    	docprint.document.open(); 
    	docprint.document.write('<html>'); 
    	docprint.document.write('<head>'); 
    	docprint.document.write('<title>Print Page</title>'); 
    	docprint.document.write('<link href="http://mywebsite.com/styles/basic.css" rel="stylesheet" type="text/css">'); 
    	docprint.document.write('</head>'); 
    	docprint.document.write('<body onload="self.print()">');          
    	docprint.document.write(content_body);          
    	docprint.document.write('</body>'); 
    	docprint.document.write('</html>');
    	docprint.document.close(); 
    	docprint.focus(); 
    	}
    </script>
    Notice the reference to your basic stylesheet that you'll have to create seperately (something nice and plain like black text on a white background and any fancy elements [slideshows/flash] hidden)

    Somewhere in the <body> include this link to start printing;
    Code:
    <a href="javascript:PrintPage();" title="Print this page">Print This Page</a>

    Lastly, define what content to print by wrapping it in a div with the id="print_content"
    Code:
    <div id="print_content">
    <!-- put all content to print inside this div with id="print_content" -->
    <!-- you can isolate a block of content and exclude the menus, header, footer, etc. that falls outside -->
    ... HTML content here ...
    </div>
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. The Following User Says Thank You to Beverleyh For This Useful Post:

    azoomer (08-19-2010)

  4. #3
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Just so I'm not stealing anyone's thunder, I think this is the source of the script: http://www.codeproject.com/KB/script...rint_page.aspx
    I've seen it posted on other forums though so I'm not 100% sure.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  5. #4
    Join Date
    Oct 2009
    Posts
    845
    Thanks
    14
    Thanked 189 Times in 188 Posts

    Default

    Thanks Beverleyh. Very useful

  6. #5
    Join Date
    Jun 2016
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi I am refer some useful article.I hope it will help you.


    http://trentrichardson.com/2006/12/25/print-preview-with-css-and-javascript/

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •