After an issue with the CSS not working when using the print() method, I was given the answer here that it's because it takes time for the CSS files to load, and it was correct.
So now I have an arbitrary timeout set to let the CSS files load:
<script>
let printData = '';
printData += '<head><link rel="stylesheet" href="mystylesheet.css" type="text/css" /></head>';
printData += '<body><p id="test">Hello</p></body>'
let printWindow = window.open('', 'Print', 'height=600,width=800');
printWindow.document.write(printData);
printWindow.document.close();
printWindow.focus();
function myprint() {
printWindow.print();
printWindow.close();
}
setTimeout(myprint, 1000)
</script>
But instead of waiting too much (or too little) by giving some arbitrary time, is there a way to update the code such that it will launch the print screen as soon as everything finishes loading?
onloadevent?write(), so wasn't sure how to place it here. Should I just add a separatewrite()for the CSS links with onload as seen in this post?