...

View Full Version : Javascript window.print(); Question



SheinTao
01-27-2006, 05:54 PM
Hello. I've written a Fax Cover Sheet generator web page for the company I work for. The user inputs all the information into a form and clicks 'print'. I do not know CGI, so i've accomplished the tasking using javascript to generate a pop-up window containing the formatted information from the form. However, no matter what I try, I can't get the browser to print the pop-up box instead of the main window. Can someone help me?

Here is sample code that imitates how the real page works:

**********************************************
<html>
<head>
<title>Print Test</title>

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function displayHTML(form) {
var notes = form.notes.value;
var print = window.print();

win = window.open(", ", 'popup', 'toolbar = no, status = no');
win.document.write("<h1>" + notes + "</h1>");
win.document.write("<SCRIPT LANGUAGE='JavaScript'>" + print + ";</script>");

}
// End -->
</script>

</head>

<body>

<form>
<textarea cols=50 rows=10 name="notes"></textarea><br>
<input type="button" value=" Print " onclick="displayHTML(this.form)">
</form>

</body>
</html>

********************************************

I would like the browser to print the resulting pop-up instead of the page where the user types in the information.

Can anyone tell me how to accomplish this?

Thanks in advance,
-Shein

konithomimo
01-27-2006, 06:00 PM
http://www.irt.org/script/1457.htm

SheinTao
01-27-2006, 08:42 PM
Thank you for the response, but that article shows how to open a different HTML document in a new window and have it print. The code I posted does not open a separate HTML document (with a different name than the referring page), but instead formats and creates its own pop-up window that needs to be printed.

I would be happy to call a new html page and have that print if I could work out a way of carrying over the form information from the original page...


Thanks for any advice/help you can provide.


EDIT: Here is a link to the actual page i'm designing if that helps :)

http://www.metrolightingcenters.com/MLHelper/faxcoverwhite.htm






.

Basscyst
01-27-2006, 09:49 PM
Why open a new window? You could just do something like this:




<html>
<head>
<title>Print Test</title>

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function printPage(form)
{
document.getElementById('printable').innerHTML='<h1>'+form.elements['notes'].value+'</h1>';
form.style.display='none';
window.print();
document.getElementById('printable').innerHTML+='<input type="button" onclick="showForm();" value="Again?" />';
}
function showForm()
{
document.forms[0].reset();
document.getElementById('printable').innerHTML='';
document.getElementById('form1').style.display='block';
}
// End -->
</script>

</head>

<body>

<form id="form1">
<textarea cols=50 rows=10 name="notes"></textarea><br>
<input type="button" value=" Print " onclick="printPage(this.form);return false;">
</form>
<div id="printable">
</div>

</body>
</html>



Basscyst

SheinTao
01-30-2006, 05:13 PM
Basscyst, that's great! Thank you!

I used your example and got the print working perfectly :thumbsup:

However, I can no longer figure out how to get the "Select Paper Type" drop-down navigation box to work. I can make it work if I put a new form outside of "form1", but then it gets printed on the cover sheet. What am I doing wrong here?

http://www.metrolightingcenters.com/MLHelper/test.htm



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum