3

I have a print button wich opens a new window with the content to be printed and after loading the included pictures (img-tag with src-attribute) calls window.print.

var newWindow = window.open('', 'MyTitle');
newWindow.document.open();
newWindow.document.write(myPrintContent)
newWindow.onload = function() {
    newWindow.print();
}
newWindow.document.close(); // necessary for IE >= 10
newWindow.focus(); // necessary for IE >= 10

This works fine in every tested browser (Chrome, FF, IE) except Safari where the onload is triggered before the images are loaded (The DOM content is displayed correctly.).

The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images, scripts, links and sub-frames have finished loading.

I tried to use onpageload but i get the same effect.

Is there a way to wait for all images to be loaded in safari?

Thomas
  • 2,431
  • 17
  • 22
  • I am waiting for multiple pictures within the dom, so i need a global document event (in my opinion?) – Thomas Apr 11 '17 at 09:18
  • There isn't a "global" event. Instead you could count of all the images, and on each image load add one to another counter. When the counter matches the number of images, then invoke print. – evolutionxbox Apr 11 '17 at 09:20
  • I thought this is the event that is supposed to work like i expected it? – Thomas Apr 11 '17 at 09:27
  • You'd think so, but does it work? – evolutionxbox Apr 11 '17 at 09:30
  • This point is on your site Sir ;) – Thomas Apr 11 '17 at 09:32
  • Ah. You are right, but as you're loading an empty page (when `.open` is invoked) the `onload` event fires immediately. – evolutionxbox Apr 11 '17 at 09:36
  • That means document.write works async? I dont think so. But i will handle this issue now with base64 encoded pictures in the dom so no image load is required. Simple but less elegant... – Thomas Apr 11 '17 at 09:48
  • _"document.write works async"_ that's not what I implied. Although the `onload` event doesn't even run in my example http://jsbin.com/xibeyigeqi/edit?js,output – evolutionxbox Apr 11 '17 at 09:52
  • 1
    http://stackoverflow.com/questions/3030859/detecting-the-onload-event-of-a-window-opened-with-window-open – evolutionxbox Apr 11 '17 at 10:07

0 Answers0