使用javascript在后台打印,即不弹出文档

使用javascript在后台打印,即不弹出文档,javascript,printing,window,Javascript,Printing,Window,找到要从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 Pa

找到要从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);
var-element=document.getElementById(元素id);
var newWin=window.open(“”,'Print-window','width=400,height=400,top=100,left=100');
newWin.document.open();
/*newWin.document.title=“页面链接上的阅读”*/
newWin.document.write('PageLinks上的读数'+element.innerHTML+'');
newWin.document.close();
setTimeout(函数(){newWin.close();},10);

该文档的打印是在加载()时完成的,因此我想没有它就无法完成打印。但是它可以隐藏吗?

您可以使用特定于打印的样式表来完成此操作,如不使用
窗口中所述;使用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();
}
function打印容器(内容、样式表){
var output=document.getElementById(“ifrOutput”).contentWindow;
output.document.open();
if(样式表!==未定义){
输出。文件。写入(“”);
}
输出.文件.编写(内容);
output.document.close();
output.focus();
output.print();
}
这样称呼它:

// with stylesheet
printHtml('<div>Styled markup</div>', 'printStyles.css');

// without stylesheet
printHtml('<div>Unstyled markup</div>');
//带有样式表
printHtml('Styled markup','printStyles.css');
//没有样式表
printHtml(“非样式化标记”);

您正在寻找打印文档部分的功能,而不是在没有操作系统打印对话框的情况下打印,对吗?我正在寻找一种方法来隐藏上面创建的
newWin.document
。有没有办法做到这一点?