admin管理员组文章数量:1241105
Is it possible to print something with a printer with javascript in the browser?
I want to print a receipt number, so if it's possible, what is the fastest printer so when the user clicks on a button it will print out eg. "1234" on a small paper.
Thanks
Is it possible to print something with a printer with javascript in the browser?
I want to print a receipt number, so if it's possible, what is the fastest printer so when the user clicks on a button it will print out eg. "1234" on a small paper.
Thanks
Share Improve this question asked Mar 7, 2011 at 13:25 ajsieajsie 79.8k110 gold badges284 silver badges386 bronze badges2 Answers
Reset to default 11You can't access the printer directly from Javascript but you can call window.print()
which will initiate the standard browser print behaviour. Using this, you could try two techniques to achieve what you're after:
Just before calling window.print()
inject a dynamic print stylesheet that only shows the elements with the text you're wanting to print. You would need to be careful to cleanup any previous print stylesheets. Or in fact you could just use an element <div id="printable">
which is the only visible element in your print stylesheet and insert any text to be printed in that. (Just be mindful if this is a website which users may actually want to print)
It's also possible to call print()
directly on an iframe window, which you could populate with your desired text. For example:
var iframe = document.createElement('iframe');
iframe.onload = function() {
var doc = iframe.contentDocument ? iframe.contentDocument : iframe.contentWindow.document;
doc.getElementsByTagName('body')[0].innerHTML = "<p>1234</p>";
iframe.contentWindow.focus(); // This is key, the iframe must have focus first
iframe.contentWindow.print();
}
document.getElementsByTagName('body')[0].appendChild(iframe);
You can't access print settings from within the browser.
This is due to security considerations, otherwise printers worldwide would be printing non-stop.
The only thing you can do regarding printing in javascript in call window.print();
.
本文标签: browserPrint from frontend javascriptStack Overflow
版权声明:本文标题:browser - Print from frontend javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740100096a2224417.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论