2017-06-21 11 views
1

我已其中創建一個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的步驟,resulttrue和在Firefox,resultfalse

我沒有得到彈出選擇我的打印設備,除非我設置在Firefox中斷點,雖然。

任何想法可能會導致這種情況?

+0

我看到這個答案:https://stackoverflow.com/a/25323486/4065876。也許它有幫助。 –

+1

@JoseLuis是的,這就是爲什麼我得到'結果'。如果它是錯誤的,那麼我將調用'print()'而不是'execCommand',如最後一點所示[這裏](https://stackoverflow.com/a/21336448/1189566) – sab669

+0

哦,對不起! :-( –

回答

1

我找到了解決辦法,但爲什麼它解決的問題超出了我:

function CentralPrint(controlID) 
{ 
    // New variable 
    var isFirefox = typeof InstallTrigger !== 'undefined'; 

    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(); 

    // Use a timeout function instead of just issuing the command immediately 
    setTimeout(function() 
    { 
     var iframe = document.getElementById("ErrorReportPrintingFrame"); 

     if (isFirefox != true) 
      iframe.contentWindow.document.execCommand("print", false, null); 
     else 
      iframe.contentWindow.print(); 

     document.body.removeChild(frameSet); 
    }, 500); 

    return false; 
} 

我想這一定是有些奇怪的問題與元素被刪除的打印命令的作用是什麼,它需要前做?

1

這種問題叫做race condition。您正試圖在加載完成之前獲取元素。

而不是使用超時,您可以添加onLoad事件偵聽器frameSet

frameSet.onload = function() { 
    var iframe = document.getElementById("ErrorReportPrintingFrame"); 
    ... 
}; 

或者,如果你喜歡的超時時間,你可以極有可能將其降低到0,它仍然可以工作。

+0

這對我不起作用,我試着把它分配給'frameDoc',而不是這樣,我也移動了'document.body.appendChild(frameSet);'這行是最後一件事。這被嘗試打印,想,也許有人要打印的內容實際上被寫入之前之前執行的控制檯告訴我'類型錯誤:frameSet.contentDocument是null' – sab669

+0

Firefox有一些競爭條件錯誤,通常可以得到全面與setTimeout 0.如果是在瀏覽器中的錯誤,可能是你唯一的選擇 –

相關問題