我已其中創建一個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
哦,對不起! :-( –