I made a function to print innerHTML and its stylesheet of a div
.
I'm having a little problem here though, because the div needs some external fontface files to load, the window.print()
needs a small delay, in order to wait for the font files to load completely before it executes.
So I used setTimeout()
in order to delay print()
for a few seconds, but it doesn't seem to work, when the function is executed, the printing page of the browser still loads immediately. Is there any way to improve this code?
function printdiv() {
var headstr = "<html><head><title>file_name</title></head><body>";
var footstr = "</body></html>";
var newstrstyle = document.getElementsByTagName("style")[0].innerHTML;
var newstr = document.getElementById("divID").innerHTML;
var oldstr = document.body.innerHTML;
document.body.innerHTML =
headstr + "<style>" + newstrstyle + "</style>" +"<div id='divID'>" + newstr + "</div>" + footstr;
setTimeout(window.print(), 2000); // is this right?
document.body.innerHTML = oldstr;
return false;
}