Skip to content

Instantly share code, notes, and snippets.

@nexusgx
Created July 2, 2014 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nexusgx/bc178d848b456bcb7179 to your computer and use it in GitHub Desktop.
Save nexusgx/bc178d848b456bcb7179 to your computer and use it in GitHub Desktop.
/*
* 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