Created
July 2, 2014 21:34
-
-
Save nexusgx/bc178d848b456bcb7179 to your computer and use it in GitHub Desktop.
Javascript print_str (from http://www.tiny-threads.com/blog/2014/07/02/javascript-print-string/)
This file contains 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
/* | |
* Javascript String Printer | |
* Written by Steve Koehler | |
* www.tiny-threads.com | |
*/ | |
function print_str(str){ | |
var print_name = ("printer-" + (new Date()).getTime()); //random printer name | |
var elemFrame=document.createElement('iframe'); //the printer iframe | |
var printer; //the window instance of the iframe | |
//set the iframe attributes | |
elemFrame.style.cssText = 'position:absolute;left:-9999px;width:1px;height:1px;'; | |
elemFrame.id=print_name; | |
elemFrame.name=print_name; | |
//add the iframe to the page | |
document.body.appendChild(elemFrame); | |
//grab the iframe window object, insert the string, and print | |
printer = elemFrame.contentWindow; | |
printer.document.open(); | |
printer.document.write(str); | |
printer.document.close(); | |
printer.focus(); | |
printer.print(); | |
//remove the iframe after a minute so we don't overload | |
setTimeout(function(){elemFrame.remove();},(60 * 1000)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment