View Full Version : Target iFrame with Print function
mark_s
Oct 5th 2007, 6:12 am
Would someone be kind enough to give me a javascript function that means when the user prints the website using the browser button, it only targets a specific iFrame id?
I first thought you would need a print button but someone told me it can be done so it works when the person uses the browser button.
ezpz
Oct 6th 2007, 1:04 am
You could try marking all the iFrames as non-printable with CSS:
@media print {
iframe.noprint { DISPLAY:none }
}
<iframe id="..." class="noprint" ...>
Then dynamically change the class of the iframe you want to print:
document.getElementById(printId).className="print";
// and if you want to make the last printable thing non-printable
document.getElementById(lastPrintId).className="noprint";
// remember lastPrintId for next time
lastPrintId=printId;
mark_s
Oct 6th 2007, 3:21 pm
Thank you for your suggestion and I'll certainly have to use it if that is the only way.
Does anyone have any ideas that are more simple to implement?
mark_s
Oct 9th 2007, 1:08 am
ezpc, how do I make it so everything by default doesn't print but then I can choose to only have a specific iframe print?
ezpz
Oct 9th 2007, 10:57 am
I've never tried turning everything off, but this might work:
@media print {
{ DISPLAY:none }
iframe.print { DISPLAY:block }
}
However, it might depend what elements your iframe is in - i.e. you might need some of the elements containing the iframe to also have DISPLAY set to block rather than none.
ezpz
Oct 9th 2007, 10:09 pm
And here's something that actually works...
<html>
<head>
<title>Test of just printing one iframe</title>
<style>
@media print {
iframe, p, table {DISPLAY:none;}
.print {DISPLAY:inline;}
}
</style>
</head>
<body>
<p>Some text.</p>
<iframe src="http://www.google.com"></iframe>
<br/><br/>
<table><tr><td>A table</td></tr></table>
<br/>
<iframe class="print" src="http://www.sectorprime.com"></iframe>
</body>
</html>
I'm afraid you have to put each element class that appears on your web page in the stylesheet with DISPLAY:none, but otherwise it's fine. You can apply the "print" class to any element.
mark_s
Oct 10th 2007, 3:35 am
Thanks a lot!!
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.