Firefox will not print unless you set a breakpoint?

advertisements

I have a method which creates an iframe, copies the contents of a textbox to that iframe, prints the iframe, then deletes the iframe:

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;
}

This works for IE 11, Chrome, and it works in Firefox if I set a breakpoint and step through the code.

I don't think this is relevant, but when I step through this with IE & Chrome, result is true and in Firefox, result is false.

I don't get the popup to choose my printing device unless I set a breakpoint in Firefox, though.

Any idea what might cause this?


I found a solution, but why it resolves the problem is beyond me:

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;
}

I guess it must be some bizarre issue with the element being deleted before the print command does what it needs to do?