tags:

views:

25

answers:

1

I'm using this nice printing script:

<script type="text/javascript"> 
     function PrintElem(elem) 
    { 
        Popup($(elem).text()); 
    } 
     function Popup(data)  
    { 
        var mywindow = window.open('', 'my div', 'height=400,width=600'); 
        mywindow.document.write('<html><head><title>my div</title>'); 
        /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />'); 
        mywindow.document.write('</head><body >'); 
        mywindow.document.write(data); 
        mywindow.document.write('</body></html>'); 
        mywindow.document.close(); 
        mywindow.print(); 
        return true; 
    } 
</script> 
</head> 
<body> 

It works fine except for two things:

  1. mywindow.print() is triggered before the page has completely loaded so you can't see the material in the window to be printed (if you select "Print" it prints correctly though).
  2. The print dialog box opens on top of the window blocking the contents (presuming #1 is an easy fix). How can it positioned?

Thanks - TY

+1  A: 

try

mywindow.document.body.onload = function(){
    mywindow.print();
};

or

setTimeout(function(){
    mywindow.print();
}, 100);

hope this helps!

jimbojw
Thanks "jimbojw" and "pranay" - you rock!! The setTimeout worked like a charm. I tuned it a little for my application and everything is smooth in all browsers. Using onload failed to open to the print dialog box - not sure why because the code should be working. I'm going to experiment with jQuery as well, but for now it's working great. MANY THANKS!
TraderY