Print an iFrame using document.frames in Firefox

2019-07-05 01:05发布

问题:

This is driving me crazy.

I've been through several questions on here and other websites and cannot get this to work.

I have an iFrame within my parent page, containing a PDF document.

The iFrame (and PDF) is small, so the Adobe Reader navigation is hidden making it impractical to use the Print function within Adobe Reader.

So, I want a button (which has to be in the Parent frame) which can trigger Print on the iFrame window.

print a PDF Iframe in firefox

How to print pdf in iframe using javascript in ie6?

and a few other have similar suggestions.

Using Firefox

document.getElementById('loyaltyBadge').focus();
document.getElementById('loyaltyBadge').contentWindow.print();

Does nothing

document.frames['loyaltyBadge'].focus();
document.frames['loyaltyBadge'].contentWindow.print();

Does nothing

document.frames.loyaltyBadge.focus();
document.frames.loyaltyBadge.contentWindow.print();

Returns document.frames is undefined... as does:

document.loyaltyBadge.print();

Although this works in Chrome

I don't want to auto-print on load, but I need a print button.

All the lines above are being wrapped in:

$('#printFrame').click(function () {  });

The button having an id of printFrame and the frame itself has an ID (and name) of loyaltyBadge

回答1:

The following works for me in Firefox 11:

window.frames.loyaltyBadge.focus();
window.frames.loyaltyBadge.print();