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

Opening a window and printing the contents

Status
Not open for further replies.

DreamerZ

Programmer
Jul 11, 2001
254
US
I am trying to open a window dynamically, filling it with data from a table on another window and then printing the new window. The window.print() and self.print() do nothing. Here's my code:
Code:
  function dailyPrint()
  {
    var entry = window.open('','_blank','resizable=yes,toolbar=yes,scrollbars=yes,height=600,width=700');
    var controls = '';
    var output = '<b>Entries for: ' + document.getElementById('entry_TodayDate').value + '</b><BR>\\n'
	+ document.getElementById('entry_Daily').innerHTML
	+ '\\n<BR><BR><i>Click \'PRINT\' on the browser window to print this page.<br>\\n'
	+ '<BR><a href=\'javascript:void\' onclick=\'window.close()\'>Close Window</a><BR>\\n';
    controls += '<input type=button value=Print onclick=\'window.print()\' >';
    entry.document.open();
    entry.document.write(output+controls);
  }

Everything else works fine, including the "Close Window" link. AND if I replace window.print() with window.close() in the controls variable, the window closes fine. So, I can't see anything with the code that's wrong. It must be with print(). Oh, the Print button on the browser window is active and visible and clicking on it prints the window as it should (brings up the print dialog box and allows printing).

Thoughts? Is this a security issue? HELP!

Just FYI, I'm not printing from the main window because it contains stuff I don't want on paper. If I put the print button on the main window, it works fine.

TIA,



DreamerZ
 
haha I was messing around with it a little bit, but i couldn't get it. So i just thought I'd tell ya how to print an alternate document if that helps at all.
Code:
<link rel="stylesheet" type="text/css" media="print" href="print.css">
href="..." is where what would print. What would happen is the user would click print and it wouldn't have to open that extra page and then click print. You could also do a couple of other things. Like if you really needed a button to push then in the main page just have the button that would regularly pop up the page, have it just print the page. or if your really needed the pop up window, change the onclick to
Code:
onclick="window.opener.print()"
Hope that helps a little. Sorry its kind of confusing too.
Matt
 
What is printing is not a saved file. The "pop-up" is created by the variable with document.write(output+controls).
So, I put the <link...> in the output variable.
I tried href='entry' (which is the window variable). Didn't work.
I then tried href='output' (which is the write variable). Didn't work.
I then tried entry.opener.print() after "entry.document.write(output+controls). That printed the main page, not the pop-up.
I then tried window.opener.print() in the onclick of the controls variable. That printed the main page, not the pop-up.

Honestly, I don't care if the window actually pops up or not, so I like the <link...> option. However, putting it in the variable didn't work.

Other options? Again, as I mentioned, window.close() in the variable works fine. Why does window.print() not?



DreamerZ
 
I have no idea why window.print() doens't work. The link thing won't work because you have to have a file that exists inside your web directory. I'll mess around with it something more monday.
Sorry,
Matt
 

Try outputting a correct and well-formed document to your popup instead of just starting with a "<b>" tag. Where are your html, body, and form tags?

Hope this helps,
Dan
 
I've tried a few more things including adding the appropriate HTML tags and putting the button on a form. Current code is below:
Code:
  function dailyPrint()
  {
    var entry = window.open('','_blank','resizable=yes,toolbar=yes,scrollbars=yes,height=600,width=700');
    var controls = '';
    var output = '<html><head><title></title></head><body>'
	+ '<b>Entries for: ' + document.getElementById('entry_TodayDate').value + '</b><BR>\\n'
	+ document.getElementById('entry_Daily').innerHTML
	+ '\\n<BR><BR><i>Click \'PRINT\' on the browser window to print this page.<br>\\n'
	+ '<BR><a href=\'javascript:void\' onclick=\'window.close()\'>Close Window</a><BR>\\n'
	+ '<form method=post name=frm_Print id=frm_Print action=\'index.phtml\'>'
	+ '<input type=button value=Print onclick=\'window.print()\' >'
	+ '</form></body></html>';
    entry.document.open();
    entry.document.write(output);
  }

Still no go. Click the button and nothing happens. I've also tried creating a function with proper script tags and such and attaching that to the onclick of the button. I've also tried using an onload in the body to print the document with both a call to the function and a direct window.print().

Nada. Any other thoughts? Thx,

DreamerZ


DreamerZ
 

Oh one question I shold have asked earlier - what browser and operating system are you testing this in (and what is it failing in)?

If it a Mac/Safari, I'm not sure window.print works, and if it is IE4/Win, you'll need a workaround, I believe (freely avaiable).

Hope this helps,
Dan
 
That work around is something like this.

..Inside your document
Code:
<object id="IEControl" width=0 height=0 classid="clsid:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>
..Where ever you want the button
Code:
<input type="button" onclick="IEControl.ExecWB(6,1)" value="Print">
Inorder for this to work your security settings have to be on low, it may be different in IE4 (I tested it in IE6), but it wouldn't work until I changed the settings.
Hope that helps a little
Matt
 
Well, I (or my fellow co-workers) solved this issue. Like me, you're all going to kick yourselves! :)

after
Code:
entry.document.write(output);

I needed an
Code:
entry.document.close();

Add that in the code above and it works fine. ARGH!!!

Thank you all for the suggestions.


DreamerZ
 
It seems so simple when you know how
biggrin.gif


Good to see you got it in the end! [2thumbsup]

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top