View Full Version : How to print another window?
buckbeak
10-10-2002, 10:43 AM
I have a print button which onClick will print another window..another page how to do that?? Is my code correct?
<script language="javascript">
function print_report(){
if(confirm("Are you sure want to print?")){
window.print("page2.html")
else return false;
</script>
<input type="submit" name="print" value=" Print " class="button2" onClick="return print_report()">
glenngv
10-10-2002, 10:45 AM
if that window is opened using window.open;
var win; //must be global
win = window.open(url);
then to print:
win.print()
buckbeak
10-10-2002, 11:01 AM
Hi Glen,
Thanks for replying.
Actually i have to pass a value to the next window then only print. If i use window.open(url)..then I cannot pass value to page2 which I wanted to print.
RadarBob
10-10-2002, 01:50 PM
b.b.,
Does your function above return "true" by default? I fogot if JavaScript does that. Even if it does you should return true explicitly. Just good, reliable coding technique.
Ahem... EVEN BETTTER coding would be to make the function more strictly "structured programming". A basic principle says "enter at the top and exit at the bottom." This means in practical terms, don't have multiple "return" statements.
No, doesn't make lots of diff. in this small example. But my eye-opening experience with rewriting some thousands of lines of old programs at work really really really made me a believer.
Below is how it could be written. Notice the code is measureably simpler because the IF is no longer a compound statement.
<script language="javascript">
function print_report(){
var passback = new Boolean (false);
if (confirm("Are you sure want to print?")){
window.print("page2.html") ;
passback = true;
}
return passback;
} // print_report()
</script>
glenngv
10-11-2002, 02:17 AM
window.print() has no arguments. it just prints the contents of the specified window.
<script language="javascript">
function print_report(){
if(confirm("Are you sure want to print?")){
win = window.open("page2.html");
win.onload = function(){window.print()}
//or you can just call window.print() onload of the page2 itself
}
}
</script>
<input type="button" name="print" value=" Print " class="button2" onClick="print_report()">
chrismiceli
10-11-2002, 02:25 AM
if the window.print funtion can have no options then this should work, but it might not.
function test() {
window.page2'sname.print();
}
<input type="button" onClick="test()" value="hi">
glenngv
10-11-2002, 03:04 AM
print() is just a method of window object.
window objects could be:
-window
-self
-top
-window.opener
-top.frames['framename']
-parent
-etc
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.