Often times it is a business requirement to print an unopened web page from an open web page. This has caused many a headache for web developers. A quick and simple method to do this is as follows:
On the web page you are going to print, create a form that has only one item, a hidden field named dobttn with an onclick eventhandler of window.print();
<form action="" method="post" name="printform">
<input type="hidden" name="dobttn" value="" onClick="window.print();">
</form>
Then create a function that accepts the formname and time in milliseconds.
The formname argument will be used to build an eval statement that will kick off the dobttn click event. The time argument will be used by the setTimeout function that will delay the self.close(); for that long of a time. This allows the print dialog box time to appear before the window closes itself.
<script>
function doandclose(formname,time){
eval("document."+formname+".dobttn.click()");
timer = setTimeout('self.close();', time);
}
</script>
Then on the body tag, add the call to the doandclose function on the onload eventhandler
<body onload="doandclose('printform',5000);">
And finally on the web page you will print from, create a function that will open the page that you want to print. The item argument is whatever you need to pass into the page in order to process it for printing. Then in the window.open, set the height and width to 1 and left and top to 1000. This will open the window well off the screen and show up only on the taskbar which will disappear when it closes itself.
<script>
function printme(item){
var printwin = window.open("od_routing_slip.cfm?od_doc_id="+item,"myprint", "height=1 width=1 left=1000 top=1000");
}
</script>