I want to print a different content from my web page that includes an image. So is my JavaScript code:
function print() {
var w = window.open();
w.document.write(....);
// time for rendering images
setTimeout(function () { w.print(); w.close();},10);
}
Google Chrome shows a new tab, with no print dialog. When I exit the tab and return it, the print dialog shows!
When I take out the 10 ms timeout, it not shows my image (there is no time for rendering the image), but the dialog shows instantly.
Maybe, someone will tell me: Creates HTML using DOM functions like createElement
and appendChild
. So you don't need rendering time and it is neater.
Therefore I did an alternative with whole HTML content created from scratch
I've used the src
dinamic assigment for my image
ele.setAttribute("src","LogoUp.png"); // ele is an image element
instead using a static HTML with same src value
<img id="header" style="margin-left: auto;
margin-right:auto;" src="LogoUp.png" >
PS: My Chrome is updated (v.63)
Now there is no need for rendering time and I can extract the setTimeout
function call, however the image is not loading. It's very weird. I saw it in the Chrome DevTools, the official Chrome debugger tool. It shows a right string, but no image is pointed.
I'm using a local computer with Windows 7 and the image is in the same directory as the html source.
So I'm crazy ...
Update:
I also have tried, with no success: (I also saw the html in Chrome DevTools
)
c.appendChild(document.getElementById("header").cloneNode(true))
// where c is the image's parent element
It's amazing because the original image works, when using innerHTML