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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can someone get this print statement to work please

Status
Not open for further replies.

glenvern

Programmer
Apr 20, 2002
5
GB
I cant get this to work - is there something missing.? Do I need to add something..? I get
[err parent.frames[1] is null or not an object]
when I click

I am trying to print a page that is not on screen

<a href=&quot;pagetoprint.htm&quot; onClick=&quot;if (window.print) {
parent.frames[1].focus();
parent.frames[1].print();
}
return false&quot;>Click here to Print&quot;</a>


 
If you're trying to print pagetoprint.htm, this isn't the way to do it. The onclick event fires before the browser location changes, so pagetoprint.htm doesn't exist to the browser until well after the onclick handler finishes executing. If you're looking for a JavaScript solution, here's what I would suggest:

Define the following function in your <head>:
Code:
<script type=&quot;text/javascript&quot;>
  function printPage(strURL)
  {
     if(window.print)
     {
        var objNewWin;
        objNewWin = window.open(strURL,&quot;newWin&quot;,&quot;width=200,height=200&quot;);
        objNewWin.focus();
        objNewWin.print();
        objNewWin.close();
     }
  }
</script>
Then write your link this way:
Code:
<a href=&quot;pageToPrint.htm&quot; target=&quot;_new&quot; onclick=&quot;printPage('pageToPrint.htm');return false;&quot;>Print the page</a>
 
pcorreia

Thank you - it works perfectly except for the fact it brings up two print windows... Should it do this.?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top