Javascript 使用javascript打印txt文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11228280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print txt file using javascript
提问by zzzzBov
I would like to know if it is possible to print a txt file located in the server using javascript. I have noticed that window.print() just opens the print dialog for the current web page
我想知道是否可以使用 javascript 打印位于服务器中的 txt 文件。我注意到 window.print() 只是打开当前网页的打印对话框
回答by zzzzBov
You can only open the print dialog for the user, and that is as it should be. If you only want to print the text document, there are a couple ways you can trigger the print dialog for it. They require following the Same Origin Policy(your HTML and TXT files need to be in the same domain).
您只能为用户打开打印对话框,这是应该的。如果您只想打印文本文档,有几种方法可以为其触发打印对话框。它们需要遵循同源策略(您的 HTML 和 TXT 文件需要在同一个域中)。
The simplest way is to open a popup window with the text file, and call print on the window handle returned:
最简单的方法是打开一个带有文本文件的弹出窗口,并在返回的窗口句柄上调用打印:
w = window.open('text.txt');
w.print();
If you want the user to preview the text file, you could use an iframe instead:
I recommend keeping JS out of HTML, this is just for example
如果您希望用户预览文本文件,则可以使用 iframe 代替:
我建议将 JS 排除在 HTML 之外,这只是示例
<iframe id="textfile" src="text.txt"></iframe>
<button onclick="print()">Print</button>
<script type="text/javascript">
function print() {
var iframe = document.getElementById('textfile');
iframe.contentWindow.print();
}
</script>
回答by codingbiz
The JQuery option
JQuery 选项
<body>
<div id="txtdiv"></div>
<script type="text/javascript">
$('#txtdiv').load('trial.txt', function()
{
window.print(); //prints when text is loaded
});
</script>
</body>
回答by rafonets
If you just do not want to delete the contents of the page and print some text from a file, you can do so here:
如果您只是不想删除页面内容并从文件中打印一些文本,您可以在此处执行此操作:
<body>
....some tags....
<script type="text/javascript">
// or onclick function
$.load('test.txt', function( printContent ){
history.pushState( printContent, 'Print title', '/print_page' );
document.write( printContent );
if( window.print() ){
document.location = '/back_page/';
// or history.go(-1);
} else {
document.location = '/history/';
}
});
</script>
回答by Vivian River
You are correct that window.print()just opens the print dialog for the current web page.
您是正确的,window.print()只是打开当前网页的打印对话框。
I would suggest that you write JavaScript code to open a new window, load the text into that window, and then call the print()function on that window.
我建议您编写 JavaScript 代码来打开一个新窗口,将文本加载到该窗口中,然后print()在该窗口上调用该函数。
回答by Falaque
You can do this by creating a web service.
您可以通过创建 Web 服务来做到这一点。
Create a web service, and do printing stuff in the webservice.
Call the web service from JavaScript.
创建一个网络服务,并在网络服务中打印东西。
从 JavaScript 调用 Web 服务。
If you are wondering how to do printing using webservice there is a thread in stackoverflowwhich might help. Dont just look the question, browse the answer as well.
如果您想知道如何使用 webservice 进行打印,stackoverflow 中有一个线程可能会有所帮助。不要只看问题,还要浏览答案。

