Created
February 24, 2015 14:47
-
-
Save jubstuff/f6056569bea9558c3776 to your computer and use it in GitHub Desktop.
Print a page in an hidden iframe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script type="text/javascript"> | |
| function closePrint () { | |
| document.body.removeChild(this.__container__); | |
| } | |
| function setPrint () { | |
| this.contentWindow.__container__ = this; | |
| this.contentWindow.onbeforeunload = closePrint; | |
| this.contentWindow.onafterprint = closePrint; | |
| this.contentWindow.focus(); // Required for IE | |
| this.contentWindow.print(); | |
| } | |
| function printPage (sURL) { | |
| var oHiddFrame = document.createElement("iframe"); | |
| oHiddFrame.onload = setPrint; | |
| oHiddFrame.style.visibility = "hidden"; | |
| oHiddFrame.style.position = "fixed"; | |
| oHiddFrame.style.right = "0"; | |
| oHiddFrame.style.bottom = "0"; | |
| oHiddFrame.src = sURL; | |
| document.body.appendChild(oHiddFrame); | |
| } | |
| </script> | |
| <a class="dk-print-car-detail" href="#" onclick="printPage('<?php echo $listing_link; ?>');return false;" title="Stampa Modelli"><?php _e('Stampa Modelli', 'dk'); ?></a> |
This does not work in some case, it sometimes show a blank preview to print.
Very helpful, worked fine for me in Chrome. Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a million!! I was just looking for exactly that. I am more a backend/laravel guy and this would have taken me 2 hours myself to work out.