使用javascript在后台打印,即没有弹出文档
Print in the background with javascript i.e. without document pop up
找到此代码以从javascript打印。 但它会打开一个窗口,其中包含要打印的文档。 有没有办法隐藏该文档?
var element=document.getElementById(element_id);
var newWin=window.open('','Print-Window','width=400,height=400,top=100,left=100');
newWin.document.open();
/* newWin.document.title = "Readings on PageLinks"; */
newWin.document.write('<html><head><title>Readings on PageLinks</title></head><body onload="window.print()">'+element.innerHTML+'</body></html>');
newWin.document.close();
setTimeout(function(){ newWin.close(); },10);
打印是在该文档的 onload() 上完成的,所以我想没有它就无法完成打印。 但它能被隐藏吗?
您可以使用特定于打印的样式表完成此操作,如如何仅打印选定的 HTML 元素中所述?根本不要使用window.open()
;使用 CSS 类(如果需要,可以动态应用)来指定应该/不应该打印哪些元素。
将此添加到您的标记中:
<iframe id="ifrOutput" style="display:none;"></iframe>
添加以下 JavaScript 函数:
function printContainer(content, styleSheet) {
var output = document.getElementById("ifrOutput").contentWindow;
output.document.open();
if (styleSheet !== undefined) {
output.document.write('<link href="' + styleSheet + '" rel="stylesheet" type="text/css" />');
}
output.document.write(content);
output.document.close();
output.focus();
output.print();
}
并这样称呼它:
// with stylesheet
printHtml('<div>Styled markup</div>', 'printStyles.css');
// without stylesheet
printHtml('<div>Unstyled markup</div>');
相关文章:
- 从Javascript和Php变量创建Html模板文档
- 有没有一种方法可以在没有文档或jQuery的情况下使用javascript解码html实体
- 是否存在Javascript Liferay Service库的文档?如何处理错误情况
- 暂停文档-HTML、CSS、JavaScript
- 如何在外部文档中使用javascript将文本区域的内容保存为变量
- 将文档实体传递给JavaScript函数
- 如何用javascript重写html文档
- 将事件发送到javascript文档的react本机模块是否正确
- 加载的XML文档为null(Javascript)
- 用javascript从窗口获取文档,同时检查文档是否已加载
- Javascript em根据窗口/视口尺寸调整大小,不影响文档样式的text/css
- 创建自己的文档Javascript
- 你知道谷歌文档Javascript是如何进行间隔数据自动刷新的吗?
- MongoDB按日期范围查找文档-JavaScript
- ABCPdf添加文档javascript
- 如果在文档Javascript中找到URL,请替换该URL
- 我怎么能在循环中立即写入文档(javascript)
- 如何使用多个文档.JavaScript中的cookie
- 如何检测文档.javascript中的addEventListener支持
- 解析PDF文档javascript