1) CODE TITLE:
Printing an iframe from its parent without opening a popup window
2) AUTHOR NAME: Arie Molendijk
3) DESCRIPTION:
There are many scripts out there that allow us to print an iframed page 'from outside', that is, not from within the iframed page itself, but from (a link on) its parent page. Most of those scripts use popup windows into which the iframe's content is first document.written and then sent to the printer from inside the popup.
Now, there is always a chance that popup blockers prevent the scripts (using popups) to function. So I looked for a way to 'do it without popups'. I found several scripts that seemed to do the job, except that IE7 - and maybe other versions too - did not print the iframed page itself, but the parent(!) and the visible part (only) of the iframed page. This is true, for instance, for the following script found at http://stackoverflow.com/questions/2...-popup-window:
I then decided that, given the fact that printing a page from a link inside an iframe never poses a problem, the most secure way of printing an iframed page from outside is to simulate the technique used for printing from inside (simply using inbuilt function print()). Printing from outside, then, means that we make a copy of the iframed page, to which we add a print button (or something similar). After the information of the copy has been sent to the printer, the copy is removed or made invisible. Example:Code:<button onclick="printPage()">print</button> <div id="printerDiv" style="display:none"></div> <script> function printPage() { var div = document.getElementById("printerDiv"); div.innerHTML = '<iframe src="mypage.aspx" onload="this.contentWindow.print();"></iframe>'; } </script>
DEMO: see URL TO CODECode:<!DOCTYPE html> <html> <head> <title>Print Iframe From Parent</title> <style> body {font-family: verdana; font-size:12px} </style> <script> var header_friendly='<div style="position: relative; text-align: center; border-bottom: 2px solid black; font-family: verdana; font-size: 11px; padding: 5px; padding-top: 1px; color: darkred">This is a printer friendly version of the page. Click <button style="cursor: pointer" onclick="window.print(); parent.document.getElementById(\'ifr_friendly\').style.left=\'-10000px\'">PRINT</button> to print it. Click <button style="cursor: pointer" onclick="parent.document.getElementById(\'ifr_friendly\').style.left=\'-10000px\'">CLOSE</button> to close this window without printing.<\/div>' function printer_friendly(which,left,top,width,height) { frames['ifr_friendly'].document.body.innerHTML=header_friendly+frames[which].document.body.innerHTML; document.getElementById('ifr_friendly').style.left=left; document.getElementById('ifr_friendly').style.top=top; document.getElementById('ifr_friendly').style.width=width; document.getElementById('ifr_friendly').style.height=height; } </script> </head> <body><div> <!-- Order: name of the printer-friendly-iframe, left, top, width, height --> <a href="javascript: void(0)" onclick="printer_friendly('ifr', '5%', '5%', '90%', '90%')">Print Iframe</a> <h2 style="text-align: center">Printing an iframe from its parent without opening a popup window</h2> <iframe id="ifr" name="ifr" style="position:absolute; left:15%; top: 15%; width: 70%; height: 70%;" src="iframed.html"></iframe> <!-- The printer friendly window --> <iframe id="ifr_friendly" name="ifr_friendly" style="position:absolute;z-index:10000; background:white; left: -10000px; border:1px solid black" ></iframe> </div></body> </html>
4) URL TO CODE:
http://mesdomaines.nu/eendracht/prin...om_parent.html



Reply With Quote

Bookmarks