views:

1875

answers:

2

At cbjsonline.com, I'm trying to have a pdf in an iframe print automatically with javascript.

Currently, my code is - (connected to the onclick of the link that opens the iframe) - document.getElementById('fancy_frame').onload = setTimeout('window.print()',2500);

Any suggestions? This method only works in safari.

+1  A: 

Try passing a function pointer to setTimeout, instead of an expression that gets eval'd.

document.getElementById('fancy_frame').onload = setTimeout( printWindow, 2500 );

// implemented in the HTML that is loaded in 'fancy_frame'
function printWindow()
{
    window.print();
}
Adam
Thanks! That's what I tried doing, but the load times varied so far, it didn't work. Also, IE tried to print the browser window, instead of the pdf, and gave a few js errors.I might have it do that only for Firefox and Safari.
CodeJoust
A: 

OK, I decided to let the user print the page. I think iFrames with PDFs are too variable trying to print with javascript. If anyone wants to try this, this is what I'd recommend. Use the jquery load, which checks for assets, not just loading, not the onload handeler, because the delay for adobe reader varies greatly across computers. Also, try naming and focusing the iframe before printing (by name, so iframe.print(), instead of window.print()), that way it won't try to print the page your currently on. It might be a better idea just to use the scribd ipaper viewer for this application.

CodeJoust