2017-06-21 34 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.如果是在浏览器中的错误,可能是你唯一的选择 –

相关问题