Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print problem 1

Status
Not open for further replies.

aspvbnetnerd

Programmer
May 16, 2006
278
SE
When I click on the link to print. A new window is opened but onload="window.print();" is never happened.
But If click F5 then the printers is loaded.
Code:
<html>
<head>
<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('</HEAD>\n')
newwin.document.write('<BODY onload="window.print();">\n')
newwin.document.write(str)
newwin.document.write('<a href="#" onClick="window.print();">Print</a>')
newwin.document.write('</BODY>\n')
newwin.document.write('</HTML>\n')
}
</script>
</head>
<body>
<div id="print_div1" style="border:1px solid #000000">
Only the contents of this div are printed.
I have used a border to highlight the printable content. 
Item One 
Item Two
</div>
<a href="#null" onclick="printContent('print_div1')">Click to print div 1</a> 
</body>
</html>

I tried to add
Code:
newwin.document.write('<a href="#" onClick="window.print();">Print</a>')
to printContent function but it doesnt work

Can anyone please help me?

link, if you wanna se the problem

George
 
George,

Try this modification of your script:
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')
[COLOR=red]    newwin.document.write('<script type="text/javascript">window.print()</s'+'cript>\n')[/color]
    newwin.document.write('</HEAD>\n')
    newwin.document.write('<BODY>\n')
    newwin.document.write(str)
    newwin.document.write('<a href="#" onClick="window.print();">Print</a>')
    newwin.document.write('</BODY>\n')
    newwin.document.write('</HTML>\n')
  }
</script>

Tested in IE and FF ;-)
 
it might also be beneficial to use the html comment tags surrounding your html. this should really always be done. additionally, why not call window.print at the bottom of your page? something like:

Code:
<html>
<head>
<script type="text/javascript">
[red]<!--[/red]
function printContent(id){
   str=document.getElementById(id).innerHTML
   newwin=window.open('','printwin','left=100,top=100,width=400,height=400')
   [red]newwin.document.open();[/red]
   newwin.document.write('<HTML>\n<HEAD>\n')
   newwin.document.write('<title>Print Page</title>\n')
   newwin.document.write('</HEAD>\n')
   newwin.document.write('<BODY>\n')
   newwin.document.write(str)
   newwin.document.write('<a href="#" onClick="window.print();">Print</a>')
   newwin.document.write('</BODY>\n')
   newwin.document.write('</HTML>\n')
   [red]newwin.document.close();[/red]
   [red]newwin.print();[/red]
}
[red]//-->[/red]
</script>
</head>
<body>
<div id="print_div1" style="border:1px solid #000000">
Only the contents of this div are printed.
I have used a border to highlight the printable content.
Item One
Item Two
</div>
<a href="#null" onclick="printContent('print_div1')">Click to print div 1</a>
</body>
</html>



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
dkdude,

It doesn't work for me. I am using ASP files.

If I have the extension ASP then it doesn't work but if I have the extension HTML the it works OK.

George
 
cLFlaVA!

I tried your example and it worked perfect.

Thanks for the help.

George
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top