我已其中创建一个iframe,拷贝的文本框,以该iframe中的内容,将打印的IFRAME的方法,然后删除的IFRAME:除非设置断点,否则Firefox不会打印?
function CentralPrint(controlID)
{
var frameSet = document.createElement('iframe');
frameSet.name = "frameSet";
frameSet.id = "ErrorReportPrintingFrame";
frameSet.style.position = "absolute";
frameSet.style.top = "-1000000px";
document.body.appendChild(frameSet);
var frameDoc = frameSet.contentWindow ? frameSet.contentWindow : frameSet.contentDocument.document ? frameSet.contentDocument.document : frameSet.contentDocument;
frameDoc.document.open();
var content = document.getElementById(controlID).value.replace(/\n/gi, '<br>');
frameDoc.document.write('<html><head><title></title><table><tr><td>');
frameDoc.document.write(' <style type="text/css"> table tr td { font-family: Arial,Helvetica,sans-serif; } </style> </head><body>');
frameDoc.document.write(content);
frameDoc.document.write('</td> </tr> </table> </body></html>');
frameDoc.document.close();
var iframe = document.getElementById("ErrorReportPrintingFrame");
var result = iframe.contentWindow.document.execCommand("print", false, null);
if (!result)
{
iframe.contentWindow.print();
}
document.body.removeChild(frameSet);
return false;
}
这适用于IE 11,铬,和它在Firefox工作if我设置了一个断点并逐步完成代码。
我不认为这是相关的,但是当我通过这次与IE & Chrome的步骤,result
是true
和在Firefox,result
是false
。
我没有得到弹出选择我的打印设备,除非我设置在Firefox中断点,虽然。
任何想法可能会导致这种情况?
我看到这个答案:https://stackoverflow.com/a/25323486/4065876。也许它有帮助。 –
@JoseLuis是的,这就是为什么我得到'结果'。如果它是错误的,那么我将调用'print()'而不是'execCommand',如最后一点所示[这里](https://stackoverflow.com/a/21336448/1189566) – sab669
哦,对不起! :-( –