I'm currently using the javascript code below to pass an element ID to a method. This method then formats that particular ID in a separate window and prints it. However, I've encountered an issue where the print dialogue box opens up before the window completes formatting the new page. As a result, I have to cancel the print dialogue, wait for the page to format, and then initiate the print again. How can I ensure that the page finishes formatting before the print dialogue box opens? Thank you.
function printPartOfPage(elementId) {
var printHeader = document.getElementById('header');
var printContent = document.getElementById(elementId);
var windowUrl = 'NewWindow';
var uniqueName = new Date();
var windowName = 'Print' + uniqueName.getTime();
var printWindow = window.open(windowUrl, windowName, 'left=20,top=200');
printWindow.document.write('<html xmlns="http://www.w3.org/1999/xhtml"><head>');
printWindow.document.write('<link href='+sURL+'css/style.css rel="stylesheet">');
printWindow.document.write('<link href='+sURL+'css/print.css rel="stylesheet">');
printWindow.document.write('</head><body>');
printWindow.document.write(printContent.innerHTML);
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.focus();
printWindow.print();
}