Return to Snippet

Revision: 65563
at December 20, 2013 07:53 by fackz


Initial Code
<script type="text/javascript">
<!--
function printContent(id){
str=document.getElementById(id).innerHTML
newwin=window.open('','printwin','left=100,top=100,width=400,height=400')
newwin.document.write('<HTML>\n<HEAD>\n')
newwin.document.write('<TITLE>Print Page</TITLE>\n')
newwin.document.write('<script>\n')
newwin.document.write('function chkstate(){\n')
newwin.document.write('if(document.readyState=="complete"){\n')
newwin.document.write('window.close()\n')
newwin.document.write('}\n')
newwin.document.write('else{\n')
newwin.document.write('setTimeout("chkstate()",2000)\n')
newwin.document.write('}\n')
newwin.document.write('}\n')
newwin.document.write('function print_win(){\n')
newwin.document.write('window.print();\n')
newwin.document.write('chkstate();\n')
newwin.document.write('}\n')
newwin.document.write('<\/script>\n')
newwin.document.write('</HEAD>\n')
newwin.document.write('<BODY onload="print_win()">\n')
newwin.document.write(str)
newwin.document.write('</BODY>\n')
newwin.document.write('</HTML>\n')
newwin.document.close()
}
//-->
</script>

Usage:

<DIV id="print_div1">Your content</div>
<a href="#null" onclick="printContent('print_div1')">Click to print div 1</a>

On clicking the link the following sequence of events happens.

    A new window is opened
    The contents of the div are dynamically written to the new window
    Data is sent to the printer
    The new window is automatically closed (IE only)

Initial URL
http://www.huntingground.freeserve.co.uk/main/mainfram.htm?../scripts/printing.htm

Initial Description
Although there is not a simple command that allows the printing of part of a document there are a couple of methods that can be used.
You can use CSS by creating a seperate style sheet to hide any content that is not required for printing or you can use a script.
The basic idea is that because the print command prints the whole page you create a new page containing only the text you want to print and print that page.

Initial Title
Print only the contents of a div

Initial Tags

                                

Initial Language
JavaScript