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

window.print() not print 2

Status
Not open for further replies.

Kendel

Programmer
Joined
Apr 24, 2002
Messages
1,512
Location
US
Hello,

Can someone plese tell me what I am doing wrong here. I'm trying to print the page using window.print() but it doesn't work.

Code:
<script language=javascript>
function myTest(){
	var win = window.open("", "", "resizable=no,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,copyhistory=0,width=720,height=300");
	win.document.writeln("<html>\n");
	win.document.writeln("<head>\n");
	win.document.writeln("<title>&nbsp;</title>\n");
	win.document.writeln("</head>\n");
	win.document.writeln("<body>\n");
	win.document.writeln("<form>\n");
	win.document.writeln("<input type='submit' style='cursor:hand' onClick='window.print()' value='Print'>");
	win.document.writeln("<input type='button' style='cursor:hand' onclick='window.close()' value='Cancel'>");
	win.document.writeln("<br>C'mon print something!!! \n");
	win.document.writeln("<br>&nbsp;\n");
	win.document.writeln("</form>");
	win.document.writeln("</body>");
	win.document.writeln("</html>");
}
</script>

<input type="button" value="print me" onclick="myTest()">

Thanks in advance.
 
This worked for me. I changed the submit button to a button button.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]

<html>
<head>
<title>new document</title>

<script language="javascript" type="text/javascript">
<!--
function myTest(){
    var win = window.open("", "win", "resizable=no,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,copyhistory=0,width=720,height=300");
    win.document.writeln("<html>\n");
    win.document.writeln("<head>\n");
    win.document.writeln("<title>&nbsp;</title>\n");
    win.document.writeln("</head>\n");
    win.document.writeln("<body>\n");
    win.document.writeln("<form>\n");
    win.document.writeln("<input type='button' style='cursor:hand' onClick='window.print()' value='Print'>");
    win.document.writeln("<input type='button' style='cursor:hand' onclick='window.close()' value='Cancel'>");
    win.document.writeln("<br>C'mon print something!!! \n");
    win.document.writeln("<br>&nbsp;\n");
    win.document.writeln("</form>");
    win.document.writeln("</body>");
    win.document.writeln("</html>");
}
-->
</script>

<style type="text/css">

</style>

</head>

<body>

<input type="button" value="print me" onclick="myTest()">

</body>

</html>

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
That's very starnge. I had button type originally but it didn't work. Just change back to from "submit" to "button" but it still didn't work.
 

Kendel,

What browser are you using? Some don't support window.print.

You should really choose to only write the buttons if the browser supports it... Something like this:

Code:
if (window.print) win.document.writeln('<input type="button" style="cursor:hand;" onclick="window.print();" value="Print">');

Also consider doing a "win.document.open();" and "win.document.close();" before and after the writes, respectively. Doing so will ensure the code writes correctly in some browsers.

Hope this helps,
Dan
 
I'm using ie6, with that "if" the Print button is now doesn't display.

If I use window.open to open a new page, and then use use print.print in this new new page, everything works as expected.
 

Do you actually have any printers set up on the box you're trying to print from?

If the print button isn't there, it must be for a reason. If you open the file menu, do you see the option to print greyed out? What happens if you press Ctrl+P? Do you see the print dialog?

Hope this helps,
Dan
 
What else could I be wrong?
 
Yes, there are acouple printers setup on that box.
 
Got it to work this way. You should thank BRPS:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]

<html>
<head>
<title>new document</title>

<script language=javascript>
function myTest(){
    var win = window.open("", "", "resizable=no,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,copyhistory=0,width=720,height=300");
	win.document.open();
    win.document.writeln("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd\">");[/URL]
	win.document.writeln("<html><head><title>new document</title></head><body><form>");
	win.document.writeln("<input type='button' onclick='window.print();' value='print me' />");
	win.document.writeln("C'mon print something!");
	win.document.writeln("</form></body></html>");
	win.document.close();
	return false;
}
</script>

</head>

<body>
<form name="f">
<input type="button" value="print me" onclick="return myTest()">
</form>
</body>

</html>

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
Changes:

- returned a value to the caller
- opened the document for writing
- closed the document when it was done being written
- returned a value to the event

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
I copied & pasted your code and it works great! Thank you so much!!!

Now, here is a little problem The page I'm working on, it has an image and it calls the function like this:

Code:
<a href="javascript: myTest()"><img name="print<%=name%>" border="0" src="images/btn_print.gif" alt="Print records"></a>&nbsp;

How do modify it so it will work with your solution?
 
cLFlaVA,


I copied & pasted your code and it works great! Thank you so much!!!

Now, here is a little problem The page I'm working on, it has an image and it calls the function like this:

Code:
<a href="javascript: myTest()"><img name="print<%=name%>" border="0" src="images/btn_print.gif" alt="Print records"></a>&nbsp;

How do modify it so it will work with your solution?
 
Thanks again cLFlaVA.

This will work:

<a href="#" onClick="return myTest()">

By the way, who is "BRPS"?
 
BRPS = BillyRayPreachersSon, the person who gave you the guidance. I just put it together with your code.

Glad it works for you.

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
Oh yeah, thank you BillyRayPreachersSon!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top