Delay window.print() until page has loaded within an ajax request to avoid a blank print screen?

Viewed 5013

What I'm trying to do with this code is make an ajax request to something that generates barcodes and returns them to be printed. When I make the request it generates everything correctly but the print window is blank. If I cancel the print window, the underlying window has the correct barcodes to be printed. I'm suspecting that I need to delay the .print() until the child window has been fully loaded, but the solution is eluding me. Thanks in advance for any help!

var barcode = $.ajax({
     url: "barcode.php",
     method: "POST",
     data: {
            name : name,
            text : text,
            number : numLines
            },
});
var printText;
barcode.done(function( jqXHR, textStatus ) {
    printText = jqXHR;
    var WindowObject = window.open();
    WindowObject.document.writeln(printText);
    WindowObject.focus();
    WindowObject.print();

});

This is all wrapped in a $( document ).ready(function(){});

2 Answers
Related