Skip to content
Advertisement

How to override standard browser print and print an iframe by default

I have a documentation type page with an iframe inside. I’m trying to override standard browser print (Ctrl + p) to print contents of an iframe only.

I know how to print an iframe content using javascript:

JavaScript

I know how to do run javascript before printing e.g. as described here: Check for when a user has selected to print using javascript

Any advise?

Thanks

Advertisement

Answer

It’s not possible (using Javascript). There is some experimental support for user-initiated print events in modern browsers, but those are not cancelable (“simple events”) so the entire page will still print even if you interject custom code to print the frame of interest.

Given this limitation, your best bet is probably to offer users a large button that fires your custom frame printing function (see printContentFrameOnly below, fire it without arguments) and hope that they’ll use the button instead of ctrl-p.

If it would be possible, this would be the way to do it (based on this answer):

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement